Permet la déconnexion d'utilisateurs

main
Clément FRÉVILLE 2 years ago
parent 98063570e7
commit 4a59f41859

@ -17,4 +17,7 @@ $router->get('/^news\/(?<id>\d+)$/', [$user, 'viewPost']);
$router->get('/^comments\/(?<id>[\w-]+)$/', [$user, 'viewPostComments']);
$router->match('/^login$/', [$security, 'login']);
$router->match('/^register$/', [$security, 'register']);
$router->run(new \Silex\DI\DI($router))->render($router, __DIR__ . '/../' . VIEW_PATH);
$router->match('/^logout$/', [$security, 'logout']);
$di = new \Silex\DI\DI($router);
$router->run($di)->render($di, __DIR__ . '/../' . VIEW_PATH);

@ -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(''));
}
}

@ -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';

@ -10,16 +10,23 @@
<nav class="navbar" role="navigation" aria-label="main navigation">
<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item">Home</a>
<a class="navbar-item" href="<?= $router->url('') ?>">Home</a>
</div>
<div class="navbar-end">
<div class="navbar-item">
<?php if ($security->getCurrentUser() !== null) : ?>
<p class="navbar-item"><?= $security->getCurrentUser()->getLogin() ?></p>
<?php endif ?>
<div class="buttons">
<a class="button is-primary" href="<?= $router->url('register') ?>">
<strong>Sign up</strong>
</a>
<a class="button is-light" href="<?= $router->url('login') ?>">Log in</a>
<?php if ($security->getCurrentUser() === null): ?>
<a class="button is-primary" href="<?= $router->url('register') ?>">
<strong>Sign up</strong>
</a>
<a class="button is-light" href="<?= $router->url('login') ?>">Log in</a>
<?php else : ?>
<a class="button is-light" href="<?= $router->url('logout') ?>">Log out</a>
<?php endif; ?>
</div>
</div>
</div>

Loading…
Cancel
Save