Permet la déconnexion d'utilisateurs

This commit is contained in:
2022-11-30 13:14:34 +01:00
parent 98063570e7
commit 4a59f41859
4 changed files with 35 additions and 14 deletions

View File

@@ -16,9 +16,7 @@ class SecurityController
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$success = $di->getSecurity()->initLogin($_POST['login'], $_POST['password']);
if ($success) {
http_response_code(303);
header('Location: ' . $di->getRouter()->url(''));
exit();
HttpResponse::redirect($di->getRouter()->url(''));
}
$fail = !$success;
}
@@ -31,12 +29,16 @@ class SecurityController
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password']));
if ($user !== null) {
http_response_code(303);
header('Location: ' . $di->getRouter()->url(''));
exit();
HttpResponse::redirect($di->getRouter()->url(''));
}
$fail = $user === null;
}
return HttpResponse::found('register', ['fail' => $fail]);
}
public function logout(DI $di): void
{
$di->getSecurity()->logout();
HttpResponse::redirect($di->getRouter()->url(''));
}
}

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace Silex\Http;
use Silex\Router\Router;
use Silex\DI\DI;
class HttpResponse
{
@@ -21,13 +21,22 @@ class HttpResponse
$this->viewParams = $viewParams;
}
public static function redirect(string $url): void
{
http_response_code(303);
header('Location: ' . $url);
exit();
}
public static function found(string $viewPath, array $viewParams = []): HttpResponse
{
return new HttpResponse(200, $viewPath, $viewParams);
}
public function render(Router $router, string $viewBasePath)
public function render(DI $di, string $viewBasePath)
{
$router = $di->getRouter();
$security = $di->getSecurity();
$params = $this->viewParams;
ob_start();
require $viewBasePath . '/' . $this->viewPath . '.php';