microserviciile de register, login finalizate
This commit is contained in:
Regular → Executable
+27
-27
@@ -1,28 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class ContactForm extends Model{
|
||||
public string $subject = '';
|
||||
public string $body = '';
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'subject' => [self::RULE_REQUIRED],
|
||||
'body' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels(): array{
|
||||
return [
|
||||
'subject' => 'Enter your subject',
|
||||
'body' => 'Enter the body'
|
||||
];
|
||||
}
|
||||
|
||||
public function send(){
|
||||
return true;
|
||||
}
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class ContactForm extends Model{
|
||||
public string $subject = '';
|
||||
public string $body = '';
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'subject' => [self::RULE_REQUIRED],
|
||||
'body' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels(): array{
|
||||
return [
|
||||
'subject' => 'Enter your subject',
|
||||
'body' => 'Enter the body'
|
||||
];
|
||||
}
|
||||
|
||||
public function send(){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Application;
|
||||
use app\core\Model;
|
||||
|
||||
class LoginForm extends Model{
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function rules(): array{
|
||||
return [
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
|
||||
'password' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels():array {
|
||||
return [
|
||||
'email' => 'Email',
|
||||
'password' => 'Password'
|
||||
];
|
||||
}
|
||||
|
||||
public function login(){
|
||||
$user = new User();
|
||||
$user = $user->findOne(['email' => $this->email]);
|
||||
if(!$user){
|
||||
$this->addError('email', 'User does not exist with this email');
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!password_verify($this->password, $user->password)){
|
||||
$this->addError('password', 'Incorrect password');
|
||||
return false;
|
||||
}
|
||||
|
||||
return Application::$app->login($user);
|
||||
}
|
||||
}
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class LoginModel extends Model{
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
|
||||
'password' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels():array
|
||||
{
|
||||
return [
|
||||
'email' => 'Email',
|
||||
'password' => 'Password'
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function createBody(): array
|
||||
{
|
||||
$body = array();
|
||||
$body["values"] = [
|
||||
"email" => $this->email,
|
||||
"password" => $this->password
|
||||
];
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function createHeaders(string $body): array
|
||||
{
|
||||
$headers = array();
|
||||
$headers["Content-type"] = "application/json";
|
||||
$headers["Content-Length"] = strlen($body);
|
||||
$headers["Authorization"] = hash('md5', 'api.knowyourfood');
|
||||
$headers["Auth-Request"] = "login";
|
||||
|
||||
return $this->formatHeaderArray($headers);
|
||||
}
|
||||
|
||||
protected function formatHeaderArray(array $headers): array{
|
||||
$array = array();
|
||||
|
||||
foreach ($headers as $key => $value){
|
||||
$array[] = $key . ": " . $value;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\Model;
|
||||
|
||||
class RegisterModel extends Model{
|
||||
public string $username = '';
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function rules(): array{
|
||||
return [
|
||||
'username' => [self::RULE_REQUIRED],
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
|
||||
'password' => [self::RULE_REQUIRED]
|
||||
];
|
||||
}
|
||||
|
||||
public function labels():array {
|
||||
return [
|
||||
'username' => 'Username',
|
||||
'email' => 'Email',
|
||||
'password' => 'Password',
|
||||
];
|
||||
}
|
||||
|
||||
public function createBody(): array
|
||||
{
|
||||
$body = array();
|
||||
$body["values"] = [
|
||||
"username" => $this->username,
|
||||
"email" => $this->email,
|
||||
"password" => $this->password
|
||||
];
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function createHeaders(string $body): array
|
||||
{
|
||||
$headers = array();
|
||||
$headers["Content-type"] = "application/json";
|
||||
$headers["Content-Length"] = strlen($body);
|
||||
$headers["Authorization"] = hash('md5', 'api.knowyourfood');
|
||||
$headers["Auth-Request"] = "register";
|
||||
|
||||
return $this->formatHeaderArray($headers);
|
||||
}
|
||||
|
||||
protected function formatHeaderArray(array $headers): array{
|
||||
$array = array();
|
||||
|
||||
foreach ($headers as $key => $value){
|
||||
$array[] = $key . ": " . $value;
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace app\models;
|
||||
use app\core\DbModel;
|
||||
|
||||
|
||||
class User extends DbModel{
|
||||
const STATUS_INACTIVE = 0;
|
||||
const STATUS_ACTIVE = 1;
|
||||
CONST STATUS_DELETED = 2;
|
||||
|
||||
public string $firstname = '';
|
||||
public string $lastname = '';
|
||||
public int $status = self::STATUS_INACTIVE;
|
||||
public string $email = '';
|
||||
public string $password = '';
|
||||
|
||||
public function save(){
|
||||
$this->status = self::STATUS_INACTIVE;
|
||||
$this->password = password_hash($this->password, PASSWORD_DEFAULT);
|
||||
return parent::save();
|
||||
}
|
||||
|
||||
public function tableName(): string{
|
||||
return 'users';
|
||||
}
|
||||
|
||||
public function rules(): array{
|
||||
return [
|
||||
'firstname' => [self::RULE_REQUIRED],
|
||||
'lastname' => [self::RULE_REQUIRED],
|
||||
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [self::RULE_UNIQUE, 'class' => self::class]],
|
||||
'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8], [self::RULE_MAX, 'max' => 15]]
|
||||
];
|
||||
}
|
||||
|
||||
public function primaryKey(): string{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
public function labels():array {
|
||||
return [
|
||||
'firstname' => 'First name',
|
||||
'lastname' => 'Last name',
|
||||
'email' => 'Email',
|
||||
'password' => 'Password'
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes(): array{
|
||||
return ['firstname', 'lastname', 'email', 'password', 'status'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user