slovocast/app/src/Middleware/AuthenticatedMiddleware.php

32 lines
969 B
PHP

<?php
namespace Slovocast\Middleware;
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;
use Odan\Session\SessionInterface;
class AuthenticatedMiddleware implements MiddlewareInterface
{
public function __construct(
private SessionInterface $session,
private ResponseFactoryInterface $responseFactory
) {}
public function process(Request $request, RequestHandler $handler): Response
{
if ($this->session->has('user')) {
$userSession = $this->session->get('user');
if ($userSession['authenticated']) {
return $handler->handle($request);
}
}
$response = $this->responseFactory->createResponse(403, "Unauthorized");
return $response;
}
}