You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.9 KiB
80 lines
1.9 KiB
<?php
|
|
|
|
namespace App\Model;
|
|
|
|
use App\Gateway\AuthGateway;
|
|
use App\Validation\FieldValidationFail;
|
|
use App\Validation\ValidationFail;
|
|
|
|
class AuthModel {
|
|
private AuthGateway $gateway;
|
|
/**
|
|
* @param AuthGateway $gateway
|
|
*/
|
|
public function __construct(AuthGateway $gateway) {
|
|
$this->gateway = $gateway;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $username
|
|
* @param string $password
|
|
* @param string $confirmPassword
|
|
* @param string $email
|
|
* @return ValidationFail[]
|
|
*/
|
|
public function register(string $username, string $password, string $confirmPassword, string $email): array {
|
|
$errors = [];
|
|
|
|
if ($password != $confirmPassword) {
|
|
$errors[] = new FieldValidationFail("confirmpassword", "password and password confirmation are not equals");
|
|
}
|
|
|
|
if ($this->gateway->mailExist($email)) {
|
|
$errors[] = new FieldValidationFail("email", "email already exist");
|
|
}
|
|
|
|
if(empty($errors)) {
|
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
|
$this->gateway->insertAccount($username, $hash, $email);
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @return array<string,string>|null
|
|
*/
|
|
public function getUserFields(string $email): ?array {
|
|
return $this->gateway->getUserFields($email);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param string $email
|
|
* @param string $password
|
|
* @return ValidationFail[] $errors
|
|
*/
|
|
public function login(string $email, string $password): array {
|
|
$errors = [];
|
|
|
|
if (!$this->gateway->mailExist($email)) {
|
|
$errors[] = new FieldValidationFail("email", "email doesnt exists");
|
|
return $errors;
|
|
}
|
|
$hash = $this->gateway->getUserHash($email);
|
|
|
|
if (!password_verify($password, $hash)) {
|
|
$errors[] = new FieldValidationFail("password", "invalid password");
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|