Now that sessions work, flash messages do not.

This commit is contained in:
Dave Smith-Hayes 2024-11-24 21:59:42 -05:00
parent 89a6d50e5f
commit 107e55175f
4 changed files with 36 additions and 4 deletions

View File

@ -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));
},
/**

View File

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

View File

@ -0,0 +1,24 @@
<?php
namespace Slovocast\Controller\User;
use Slovocast\Controller\Controller;
use Psr\Http\Message\ResponseInterface as Response;
use Odan\Session\SessionInterface;
class LogoutUserAction extends Controller
{
public function __construct(
protected SessionInterface $session
) { }
public function handle(): Response
{
if ($this->session->has('user')) {
$this->session->delete('user');
}
return $this->redirect('/', 302);
}
}

View File

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