From 037cf674a8e9f8e1c4b5dd296dfa0250bbe56e12 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Thu, 27 Jun 2024 19:50:42 -0400 Subject: [PATCH] Add a dashboard controller. --- app/src/Controller/DashboardPage.php | 19 ++++++++++++ .../Middleware/AuthenticatedMiddleware.php | 10 ++++--- app/src/Routes.php | 29 ++++++++++++------- 3 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 app/src/Controller/DashboardPage.php diff --git a/app/src/Controller/DashboardPage.php b/app/src/Controller/DashboardPage.php new file mode 100644 index 0000000..6ad097e --- /dev/null +++ b/app/src/Controller/DashboardPage.php @@ -0,0 +1,19 @@ +render('dashboard.twig'); + } +} diff --git a/app/src/Middleware/AuthenticatedMiddleware.php b/app/src/Middleware/AuthenticatedMiddleware.php index d38f29a..816dbe3 100644 --- a/app/src/Middleware/AuthenticatedMiddleware.php +++ b/app/src/Middleware/AuthenticatedMiddleware.php @@ -18,11 +18,13 @@ class AuthenticatedMiddleware implements MiddlewareInterface public function process(Request $request, RequestHandler $handler): Response { - if (!this->session->has('user')) { - $response = $this->responseFactory->createResponse(403, "Unauthorized"); - return $response; + if ($this->session->has('user')) { + if ($this->session->get('user')['authenticated']) { + return $handler->handle($request); + } } - return $handler->handle($request); + $response = $this->responseFactory->createResponse(403, "Unauthorized"); + return $response; } } diff --git a/app/src/Routes.php b/app/src/Routes.php index bbbd717..99e4755 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -5,19 +5,20 @@ namespace Slovocast; use Slim\App; use Slovocast\Controller\HomePage; use Slovocast\Controller\HealthCheck; -use Slovocast\Controller\User\{ - RegisterUserPage, - RegisterUserAction, - LoginUserPage, - LoginUserAction -}; -use Slovocast\Middleware\{ - VerifyPasswordMiddleware, - AuthenticatedMiddleware -}; +use Slovocast\Controller\DashboardPage; +use Slovocast\Controller\User\RegisterUserPage; +use Slovocast\Controller\User\RegisterUserAction; +use Slovocast\Controller\User\LoginUserPage; +use Slovocast\Controller\User\LoginUserAction; +use Slovocast\Middleware\VerifyPasswordMiddleware; +use Slovocast\Middleware\AuthenticatedMiddleware; class Routes { + /** + * @param App $app Instantiated Application + * @return void + */ public static function init(App $app): void { $app->get('/', HomePage::class); @@ -26,6 +27,10 @@ class Routes self::users($app); } + /** + * @param App $app Instantiated Application + * @return void + */ protected static function users(App $app): void { $app->get('/users/register', RegisterUserPage::class) @@ -39,5 +44,9 @@ class Routes $app->post('/login', LoginUserAction::class) ->add(AuthenticatedMiddleware::class) ->setName('user-login-action'); + + $app->get('/dashboard', DashboardPage::class) + ->add(AuthenticatedMiddleware::class) + ->setName('user-dashboard'); } }