From 98c34efea5ffd4aadcd23f14f70fa242381efbde Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Sun, 16 Jun 2024 20:44:12 -0400 Subject: [PATCH] Add a Slim Response factory to the DI. --- app/src/Bootstrap.php | 5 +++++ app/src/Infrastructure/DatabaseConnection.php | 12 ++++-------- .../User/UserAuthorizationInterface.php | 3 +++ app/src/Middleware/VerifyPasswordMiddleware.php | 14 +++++++++----- app/src/Routes.php | 12 +++++++++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 8b9a6b9..9ce7cc5 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -8,11 +8,13 @@ use Slim\Factory\AppFactory; use League\Config\Configuration; use Psr\Container\ContainerInterface; +use Psr\Http\Message\ResponseFactoryInterface; use DI\Container; use DI\ContainerBuilder; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; +use Slim\Psr7\Factory\ResponseFactory; use Slovocast\Routes; use Slovocast\Configuration\SiteInformationSchema; @@ -89,6 +91,9 @@ class Bootstrap SessionInterface::class => function (ContainerInterface $container) { $options = $container->get('config')->get('session'); return new PhpSession($options); + }, + ResponseFactoryInterface::class => function (ContainerInterface $container) { + return new ResponseFactory(); } ]); diff --git a/app/src/Infrastructure/DatabaseConnection.php b/app/src/Infrastructure/DatabaseConnection.php index b445e96..e0e0d2f 100644 --- a/app/src/Infrastructure/DatabaseConnection.php +++ b/app/src/Infrastructure/DatabaseConnection.php @@ -10,14 +10,10 @@ use Slovocast\Infrastructure\DatabaseConnectionInterface; */ class DatabaseConnection implements DatabaseConnectionInterface { - private string $name; - private PDO $pdo; - - public function __construct(string $name, PDO $pdo) - { - $this->name = $name; - $this->pdo = $pdo; - } + public function __construct( + private string $name, + private PDO $pdo + ) { } public function getConnection(): PDO { diff --git a/app/src/Infrastructure/User/UserAuthorizationInterface.php b/app/src/Infrastructure/User/UserAuthorizationInterface.php index 9490524..ab7d445 100644 --- a/app/src/Infrastructure/User/UserAuthorizationInterface.php +++ b/app/src/Infrastructure/User/UserAuthorizationInterface.php @@ -2,6 +2,9 @@ namespace Slovocast\Infrastructure\User; +/** + * A simple interface for securing and checking secured passwords for a user + */ interface UserAuthorizationInterface { public function hash(string $password): string; diff --git a/app/src/Middleware/VerifyPasswordMiddleware.php b/app/src/Middleware/VerifyPasswordMiddleware.php index 9e8db47..d7521e9 100644 --- a/app/src/Middleware/VerifyPasswordMiddleware.php +++ b/app/src/Middleware/VerifyPasswordMiddleware.php @@ -7,11 +7,13 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface as RequestHandler; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ResponseFactoryInterface; class VerifyPasswordMiddleware implements MiddlewareInterface { public function __construct( - private SessionInterface $session + private SessionInterface $session, + private ResponseFactoryInterface $responseFactory ) {} public function process(Request $request, RequestHandler $handler): Response @@ -20,12 +22,14 @@ class VerifyPasswordMiddleware implements MiddlewareInterface $requestData = $request->getParsedBody(); if ($requestData['password'] !== $requestData['checked_password']) { - + $message = "Passwords do not match."; $this->session ->getFlash() - ->add('error', "Passwords do not match."); - $response = $handler->handle($request); - return $response->withStatus(400); + ->add('error', $message); + + // throw an error? send it back to the register screen? + $response = $this->responseFactory->createResponse(400, $message); + return $response; } return $handler->handle($request); diff --git a/app/src/Routes.php b/app/src/Routes.php index 6e1b6c7..48dafc7 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -6,7 +6,8 @@ use Slim\App; use Slovocast\Controller\HomePage; use Slovocast\Controller\User\{ RegisterUserPage, - RegisterUserAction + RegisterUserAction, + LoginUserPage }; use Slovocast\Middleware\VerifyPasswordMiddleware; @@ -21,8 +22,13 @@ class Routes protected static function users(App $app): void { - $app->get('/users/register', RegisterUserPage::class); + $app->get('/users/register', RegisterUserPage::class) + ->setName('user-register-page'); $app->post('/users/register', RegisterUserAction::class) - ->add(VerifyPasswordMiddleware::class); + ->add(VerifyPasswordMiddleware::class) + ->setName('user-register-action'); + + $app->get('/login', LoginUserPage::class) + ->setName('user-login-page'); } }