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.
83 lines
2.9 KiB
83 lines
2.9 KiB
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Gateway\AuthGateway;
|
|
use App\Http\HttpRequest;
|
|
use App\Http\HttpResponse;
|
|
use App\Http\ViewHttpResponse;
|
|
use App\Model\AuthModel;
|
|
use App\Validation\FieldValidationFail;
|
|
use App\Validation\Validators;
|
|
use Twig\Environment;
|
|
|
|
|
|
class AuthController {
|
|
private AuthModel $model;
|
|
|
|
/**
|
|
* @param AuthModel $model
|
|
*/
|
|
public function __construct(AuthModel $model) {
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function displayRegister(): HttpResponse {
|
|
return ViewHttpResponse::twig("display_register.html.twig", []);
|
|
}
|
|
|
|
private function displayBadFields(string $viewName, array $fails): HttpResponse{
|
|
$bad_fields = [];
|
|
foreach ($fails as $err) {
|
|
if ($err instanceof FieldValidationFail) {
|
|
$bad_fields[] = $err->getFieldName();
|
|
}
|
|
}
|
|
return ViewHttpResponse::twig($viewName, ['bad_fields' => $bad_fields]);
|
|
}
|
|
|
|
|
|
public function confirmRegister(array $request): HttpResponse {
|
|
$fails = [];
|
|
$request = HttpRequest::from($request, $fails, [
|
|
"username" => [Validators::name(), Validators::lenBetween(2, 32)],
|
|
"password" => [Validators::lenBetween(6, 256)],
|
|
"confirmpassword" => [Validators::lenBetween(6, 256)],
|
|
"email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)]
|
|
]);
|
|
if (!empty($fails)) {
|
|
return $this->displayBadFields("display_register.html.twig",$fails);
|
|
}
|
|
$fails = $this->model->register($request['username'], $request["password"], $request['confirmpassword'], $request['email']);
|
|
if (empty($fails)) {
|
|
$results = $this->model->getUserFields($request['email']);
|
|
return ViewHttpResponse::twig("display_auth_confirm.html.twig", ['username' => $results['username'], 'email' => $results['email']]);
|
|
}
|
|
return $this->displayBadFields("display_register.html.twig",$fails);
|
|
}
|
|
|
|
|
|
public function displayLogin():HttpResponse{
|
|
return ViewHttpResponse::twig("display_login.html.twig", []);
|
|
}
|
|
|
|
|
|
public function confirmLogin(array $request):HttpResponse{
|
|
$fails = [];
|
|
$request = HttpRequest::from($request, $fails, [
|
|
"password" => [Validators::lenBetween(6, 256)],
|
|
"email" => [Validators::regex("/^\\S+@\\S+\\.\\S+$/"),Validators::lenBetween(5, 256)]
|
|
]);
|
|
if (!empty($fails)) {
|
|
return $this->displayBadFields("display_login.html.twig",$fails);
|
|
}
|
|
|
|
$fails = $this->model->login($request['email'],$request['password']);
|
|
if (empty($fails)){
|
|
$results = $this->model->getUserFields($request['email']);
|
|
return ViewHttpResponse::twig("display_auth_confirm.html.twig",['username' => $results['username'], 'email' => $results['email']]);
|
|
}
|
|
return $this->displayBadFields("display_login.html.twig",$fails);
|
|
}
|
|
|
|
} |