Add some middleware, start failing tests that use it.
This commit is contained in:
parent
ea97887cac
commit
596a2a4103
@ -109,7 +109,9 @@ class Bootstrap
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Taking an instantiated application, sets up all the middleware required
|
* 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
|
* @param App $app
|
||||||
*/
|
*/
|
||||||
|
@ -17,25 +17,16 @@ class RegisterUserAction extends Controller
|
|||||||
|
|
||||||
public function respond(): Response
|
public function respond(): Response
|
||||||
{
|
{
|
||||||
// get user Data
|
|
||||||
$requestData = $this->request->getParsedBody();
|
$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([
|
$user = User::fromArray([
|
||||||
'email' => $requestData['email'],
|
'email' => $requestData['email'],
|
||||||
'name' => $requestData['name'],
|
'name' => $requestData['name'],
|
||||||
'password' => $requestData['password'],
|
'password' => $requestData['password'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$result = $this->userRepository->save($user);
|
$success = $this->userRepository->save($user);
|
||||||
|
|
||||||
if ($result) {
|
if ($success) {
|
||||||
return $this->render('user/success.twig');
|
return $this->render('user/success.twig');
|
||||||
} else {
|
} else {
|
||||||
$this->session
|
$this->session
|
||||||
@ -43,6 +34,5 @@ class RegisterUserAction extends Controller
|
|||||||
->add('error', "Unable to register user.");
|
->add('error', "Unable to register user.");
|
||||||
return $this->render('user/register.twig')->withStatus(400);
|
return $this->render('user/register.twig')->withStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
app/src/Infrastructure/Middleware.php
Normal file
38
app/src/Infrastructure/Middleware.php
Normal 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;
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ use Slovocast\Controller\User\{
|
|||||||
RegisterUserPage,
|
RegisterUserPage,
|
||||||
RegisterUserAction
|
RegisterUserAction
|
||||||
};
|
};
|
||||||
|
use Slovocast\Infrastructure\Middleware\VerifyPasswordMiddleware;
|
||||||
|
|
||||||
class Routes
|
class Routes
|
||||||
{
|
{
|
||||||
@ -21,6 +22,7 @@ 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);
|
||||||
$app->post('/users/register', RegisterUserAction::class);
|
$app->post('/users/register', RegisterUserAction::class)
|
||||||
|
->add(new VerifyPasswordMiddleware());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user