Add a Slim Response factory to the DI.

This commit is contained in:
Dave Smith-Hayes 2024-06-16 20:44:12 -04:00
parent 4ff2c8cc3a
commit 98c34efea5
5 changed files with 30 additions and 16 deletions

View File

@ -8,11 +8,13 @@ use Slim\Factory\AppFactory;
use League\Config\Configuration; use League\Config\Configuration;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use Slim\Views\Twig; use Slim\Views\Twig;
use Slim\Views\TwigMiddleware; use Slim\Views\TwigMiddleware;
use Slim\Psr7\Factory\ResponseFactory;
use Slovocast\Routes; use Slovocast\Routes;
use Slovocast\Configuration\SiteInformationSchema; use Slovocast\Configuration\SiteInformationSchema;
@ -89,6 +91,9 @@ class Bootstrap
SessionInterface::class => function (ContainerInterface $container) { SessionInterface::class => function (ContainerInterface $container) {
$options = $container->get('config')->get('session'); $options = $container->get('config')->get('session');
return new PhpSession($options); return new PhpSession($options);
},
ResponseFactoryInterface::class => function (ContainerInterface $container) {
return new ResponseFactory();
} }
]); ]);

View File

@ -10,14 +10,10 @@ use Slovocast\Infrastructure\DatabaseConnectionInterface;
*/ */
class DatabaseConnection implements DatabaseConnectionInterface class DatabaseConnection implements DatabaseConnectionInterface
{ {
private string $name; public function __construct(
private PDO $pdo; private string $name,
private PDO $pdo
public function __construct(string $name, PDO $pdo) ) { }
{
$this->name = $name;
$this->pdo = $pdo;
}
public function getConnection(): PDO public function getConnection(): PDO
{ {

View File

@ -2,6 +2,9 @@
namespace Slovocast\Infrastructure\User; namespace Slovocast\Infrastructure\User;
/**
* A simple interface for securing and checking secured passwords for a user
*/
interface UserAuthorizationInterface interface UserAuthorizationInterface
{ {
public function hash(string $password): string; public function hash(string $password): string;

View File

@ -7,11 +7,13 @@ use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler; use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ResponseFactoryInterface;
class VerifyPasswordMiddleware implements MiddlewareInterface class VerifyPasswordMiddleware implements MiddlewareInterface
{ {
public function __construct( public function __construct(
private SessionInterface $session private SessionInterface $session,
private ResponseFactoryInterface $responseFactory
) {} ) {}
public function process(Request $request, RequestHandler $handler): Response public function process(Request $request, RequestHandler $handler): Response
@ -20,12 +22,14 @@ class VerifyPasswordMiddleware implements MiddlewareInterface
$requestData = $request->getParsedBody(); $requestData = $request->getParsedBody();
if ($requestData['password'] !== $requestData['checked_password']) { if ($requestData['password'] !== $requestData['checked_password']) {
$message = "Passwords do not match.";
$this->session $this->session
->getFlash() ->getFlash()
->add('error', "Passwords do not match."); ->add('error', $message);
$response = $handler->handle($request);
return $response->withStatus(400); // throw an error? send it back to the register screen?
$response = $this->responseFactory->createResponse(400, $message);
return $response;
} }
return $handler->handle($request); return $handler->handle($request);

View File

@ -6,7 +6,8 @@ use Slim\App;
use Slovocast\Controller\HomePage; use Slovocast\Controller\HomePage;
use Slovocast\Controller\User\{ use Slovocast\Controller\User\{
RegisterUserPage, RegisterUserPage,
RegisterUserAction RegisterUserAction,
LoginUserPage
}; };
use Slovocast\Middleware\VerifyPasswordMiddleware; use Slovocast\Middleware\VerifyPasswordMiddleware;
@ -21,8 +22,13 @@ class Routes
protected static function users(App $app): void 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) $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');
} }
} }