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.
91 lines
2.9 KiB
91 lines
2.9 KiB
<?php
|
|
|
|
namespace IQBall\App\Controller;
|
|
|
|
use IQBall\App\Session\MutableSessionHandle;
|
|
use IQBall\App\ViewHttpResponse;
|
|
use IQBall\Core\Http\HttpRequest;
|
|
use IQBall\Core\Http\HttpResponse;
|
|
use IQBall\Core\Model\AuthModel;
|
|
use IQBall\Core\Validation\Validators;
|
|
|
|
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", []);
|
|
}
|
|
|
|
/**
|
|
* registers given account
|
|
* @param mixed[] $request
|
|
* @param MutableSessionHandle $session
|
|
* @return HttpResponse
|
|
*/
|
|
public function register(array $request, MutableSessionHandle $session): HttpResponse {
|
|
$fails = [];
|
|
HttpRequest::from($request, $fails, [
|
|
"username" => [Validators::name(), Validators::lenBetween(2, 32)],
|
|
"password" => [Validators::lenBetween(6, 256)],
|
|
"confirmpassword" => [Validators::lenBetween(6, 256)],
|
|
"email" => [Validators::email(), Validators::lenBetween(5, 256)],
|
|
]);
|
|
|
|
if (!empty($fails)) {
|
|
if (!(in_array($request['username'], $fails)) or !(in_array($request['email'], $fails))) {
|
|
return ViewHttpResponse::twig("display_register.html.twig", ['fails' => $fails,'username' => $request['username'],'email' => $request['email']]);
|
|
}
|
|
}
|
|
|
|
$account = $this->model->register($request['username'], $request["password"], $request['confirmpassword'], $request['email'], $fails);
|
|
if (!empty($fails)) {
|
|
return ViewHttpResponse::twig("display_register.html.twig", ['fails' => $fails]);
|
|
}
|
|
$session->setAccount($account);
|
|
|
|
$target_url = $session->getInitialTarget();
|
|
if ($target_url != null) {
|
|
return HttpResponse::redirect_absolute($target_url);
|
|
}
|
|
|
|
return HttpResponse::redirect("/home");
|
|
}
|
|
|
|
|
|
public function displayLogin(): HttpResponse {
|
|
return ViewHttpResponse::twig("display_login.html.twig", []);
|
|
}
|
|
|
|
/**
|
|
* logins given account credentials
|
|
* @param mixed[] $request
|
|
* @param MutableSessionHandle $session
|
|
* @return HttpResponse
|
|
*/
|
|
public function login(array $request, MutableSessionHandle $session): HttpResponse {
|
|
$fails = [];
|
|
$account = $this->model->login($request['email'], $request['password'], $fails);
|
|
if (!empty($fails)) {
|
|
return ViewHttpResponse::twig("display_login.html.twig", ['fails' => $fails]);
|
|
}
|
|
|
|
$session->setAccount($account);
|
|
|
|
$target_url = $session->getInitialTarget();
|
|
$session->setInitialTarget(null);
|
|
if ($target_url != null) {
|
|
return HttpResponse::redirect_absolute($target_url);
|
|
}
|
|
|
|
return HttpResponse::redirect("/home");
|
|
}
|
|
|
|
}
|