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 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();
}
]);

View File

@ -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
{

View File

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

View File

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

View File

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