blob: 1985a0c861ae3ab49e5b55c37ac2e2acf563bc74 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?php
declare(strict_types=1);
namespace Nsfisis\Albatross\Middlewares;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\App;
final class AuthRequiredMiddleware implements MiddlewareInterface
{
private function __construct(
private readonly ResponseFactoryInterface $responseFactory,
private readonly string $loginPath,
) {
}
public static function create(
App $app,
string $loginRouteName,
): self {
return new self(
$app->getResponseFactory(),
$app->getRouteCollector()->getRouteParser()->urlFor($loginRouteName),
);
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$current_user = $request->getAttribute('current_user');
if ($current_user === null) {
return $this->responseFactory
->createResponse(302)
->withHeader('Location', $this->loginPath . "?to=" . urlencode($request->getUri()->getPath()));
}
return $handler->handle($request);
}
}
|