slovocast/app/src/Middleware/FormKeyMiddleware.php

28 lines
749 B
PHP

<?php
namespace Slovocast\Middleware;
use Odan\Session\SessionInterface;
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 Ramsey\Uuid\Uuid;
class FormKeyMiddleware implements MiddlewareInterface
{
public function __construct(
private SessionInterface $session
) { }
public function process(Request $request, RequestHandler $handler): Response
{
if (!$this->session->has("form_key")) {
$uuid = Uuid::uuid4();
$this->session->set("form_key", $uuid->toString());
}
return $handler->handle($request);
}
}