diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index a0903c0..728ec4c 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -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 */ diff --git a/app/src/Controller/User/RegisterUserAction.php b/app/src/Controller/User/RegisterUserAction.php index c5a445f..947bbe6 100644 --- a/app/src/Controller/User/RegisterUserAction.php +++ b/app/src/Controller/User/RegisterUserAction.php @@ -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); } - } } diff --git a/app/src/Infrastructure/Middleware.php b/app/src/Infrastructure/Middleware.php new file mode 100644 index 0000000..8447b69 --- /dev/null +++ b/app/src/Infrastructure/Middleware.php @@ -0,0 +1,38 @@ +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; +} diff --git a/app/src/Infrastructure/Middleware/VerifyPasswordMiddleware.php b/app/src/Infrastructure/Middleware/VerifyPasswordMiddleware.php new file mode 100644 index 0000000..78d3b59 --- /dev/null +++ b/app/src/Infrastructure/Middleware/VerifyPasswordMiddleware.php @@ -0,0 +1,28 @@ +getParsedBody(); + + if ($requestData['password'] !== $requestData['checked_password']) { + $this->session->getFlash() + ->add('error', "Passwords do not match."); + return $response->withStatus(400); + } + + return $response; + } +} diff --git a/app/src/Routes.php b/app/src/Routes.php index 7cda3f5..ab3a6fd 100644 --- a/app/src/Routes.php +++ b/app/src/Routes.php @@ -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()); } }