From 107e55175fe10fcf471f4e8a7598fc34f1fcd628 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Sun, 24 Nov 2024 21:59:42 -0500 Subject: [PATCH] Now that sessions work, flash messages do not. --- app/src/Bootstrap.php | 6 ++--- app/src/Controller/User/LoginUserAction.php | 6 ++++- app/src/Controller/User/LogoutUserAction.php | 24 ++++++++++++++++++++ app/src/Routes.php | 4 ++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 app/src/Controller/User/LogoutUserAction.php diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index d95d1e0..8093884 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -113,8 +113,8 @@ class Bootstrap * Global logging */ LoggerInterface::class => function() { - $logger = new Logger(); - $logger->pushHandler(new StreamHandler(APP_LOGS_DIR, Level::Warning)); + $logger = new Logger("default"); + $logger->pushHandler(new StreamHandler('php://stdout', Level::Warning)); return $logger; }, @@ -132,7 +132,7 @@ class Bootstrap return $container->get(SessionInterface::class)->getFlash(); }, SessionMiddleware::class => function (ContainerInterface $container) { - return new SessionMiddleware($container->get(SessionManagerInterface::class)); + return new SessionMiddleware($container->get(SessionInterface::class)); }, /** diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php index f51ba54..85f1a57 100644 --- a/app/src/Controller/User/LoginUserAction.php +++ b/app/src/Controller/User/LoginUserAction.php @@ -4,6 +4,7 @@ namespace Slovocast\Controller\User; use Odan\Session\SessionInterface; use Psr\Http\Message\ResponseInterface as Response; +use Psr\Log\LoggerInterface; use Slovocast\Controller\Controller; use Slovocast\Domain\Repository\User\UserRepositoryInterface; use Slovocast\Exception\EntityNotFoundException; @@ -14,7 +15,8 @@ class LoginUserAction extends Controller public function __construct( private UserAuthorizationInterface $auth, private UserRepositoryInterface $userRepository, - private SessionInterface $session + private SessionInterface $session, + private LoggerInterface $logger, ) { } public function handle(): Response @@ -24,11 +26,13 @@ class LoginUserAction extends Controller try { $user = $this->userRepository->getFromEmail($credentials['email']); } catch (EntityNotFoundException $e) { + $this->logger->error("Unable to login user."); $this->session->getFlash()->add('error', "Unable to login user."); return $this->render('user/login.twig')->withStatus(400); } if (!$this->auth->verify($credentials['password'], $user->getPassword())) { + $this->logger->error("Unable to verify user password."); $this->session->getFlash()->add('error', "Unable to login user."); return $this->render('user/login.twig')->withStatus(400); } diff --git a/app/src/Controller/User/LogoutUserAction.php b/app/src/Controller/User/LogoutUserAction.php new file mode 100644 index 0000000..b585192 --- /dev/null +++ b/app/src/Controller/User/LogoutUserAction.php @@ -0,0 +1,24 @@ +session->has('user')) { + $this->session->delete('user'); + } + + return $this->redirect('/', 302); + } +} diff --git a/app/src/Routes.php b/app/src/Routes.php index 134deaa..eb72594 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -10,6 +10,7 @@ use Slovocast\Controller\User\RegisterUserPage; use Slovocast\Controller\User\RegisterUserAction; use Slovocast\Controller\User\LoginUserPage; use Slovocast\Controller\User\LoginUserAction; +use Slovocast\Controller\User\LogoutUserAction; use Slovocast\Middleware\VerifyPasswordMiddleware; use Slovocast\Middleware\AuthenticatedMiddleware; @@ -43,6 +44,9 @@ class Routes ->setName('user-login-page'); $app->post('/login', LoginUserAction::class) ->setName('user-login-action'); + + $app->get('/logout', LogoutUserAction::class) + ->setName('user-logout'); } /**