mirror of
https://github.com/ceratic/MediaCollectorLibary.git
synced 2026-05-14 08:06:47 +02:00
34 lines
940 B
PHP
34 lines
940 B
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use App\Services\AuthService;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
class AuthMiddleware implements MiddlewareInterface
|
|
{
|
|
private AuthService $auth;
|
|
|
|
public function __construct(AuthService $auth)
|
|
{
|
|
$this->auth = $auth;
|
|
}
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
{
|
|
// Check if user is logged in
|
|
if (!$this->auth->isLoggedIn()) {
|
|
$response = new \Slim\Psr7\Response();
|
|
return $response->withStatus(302)->withHeader('Location', '/login');
|
|
}
|
|
|
|
// Add user to request attributes
|
|
$request = $request->withAttribute('user', $this->auth->getCurrentUser());
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|