Add some middleware, start failing tests that use it.

This commit is contained in:
Dave Smith-Hayes 2024-06-15 21:50:40 -04:00
parent ea97887cac
commit 596a2a4103
5 changed files with 74 additions and 14 deletions

View File

@ -109,7 +109,9 @@ class Bootstrap
/**
* Taking an instantiated application, sets up all the middleware required
* for the application to run.
* for the application to run. This appends Middleware in a Global sense,
* not per-route. Per-route middleware will be set in the `establishRoutes`
* method.
*
* @param App $app
*/

View File

@ -17,25 +17,16 @@ class RegisterUserAction extends Controller
public function respond(): Response
{
// get user Data
$requestData = $this->request->getParsedBody();
if ($requestData['password'] !== $requestData['checked_password']) {
$this->session->getFlash()
->add('error', "Passwords do not match.");
$response = $this->render('user/register.twig');
return $response->withStatus(400);
}
$user = User::fromArray([
'email' => $requestData['email'],
'name' => $requestData['name'],
'password' => $requestData['password'],
]);
$result = $this->userRepository->save($user);
$success = $this->userRepository->save($user);
if ($result) {
if ($success) {
return $this->render('user/success.twig');
} else {
$this->session
@ -43,6 +34,5 @@ class RegisterUserAction extends Controller
->add('error', "Unable to register user.");
return $this->render('user/register.twig')->withStatus(400);
}
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Slovocast\Infrastructure;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
abstract class Middleware
{
/**
* @param Request $request
* @param Response $response
* @param callable $next
* @return Response
*/
public function __invoke(
Request $request,
Response $response,
callable $next
): Response {
return $this->invoke($request, $response, $next);
}
/**
* To write Middleware, we need to implement this concrete method, and set
* the child class as the Middleware in the Application bootstrapping.
*
* @param Request $request
* @param Response $response
* @param callable $next
* @return Response
*/
abstract public function invoke(
Request $request,
Response $response,
callable $next
): Response;
}

View File

@ -0,0 +1,28 @@
<?php
namespace Slovocast\Infrastructure\Middleware;
use Slovocast\Infrastructure\Middleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
class VerifyPasswordMiddleware extends Middleware
{
public function invoke(
Request $request,
Response $response,
callable $next
): Response {
// get user Data
$requestData = $request->getParsedBody();
if ($requestData['password'] !== $requestData['checked_password']) {
$this->session->getFlash()
->add('error', "Passwords do not match.");
return $response->withStatus(400);
}
return $response;
}
}

View File

@ -8,6 +8,7 @@ use Slovocast\Controller\User\{
RegisterUserPage,
RegisterUserAction
};
use Slovocast\Infrastructure\Middleware\VerifyPasswordMiddleware;
class Routes
{
@ -21,6 +22,7 @@ class Routes
protected static function users(App $app): void
{
$app->get('/users/register', RegisterUserPage::class);
$app->post('/users/register', RegisterUserAction::class);
$app->post('/users/register', RegisterUserAction::class)
->add(new VerifyPasswordMiddleware());
}
}