-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpireUserSessionMiddleware.php
59 lines (48 loc) · 1.7 KB
/
ExpireUserSessionMiddleware.php
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace App\Infrastructure\Http\Middleware;
use Codefy\Framework\Auth\UserSession;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Qubus\Exception\Data\TypeException;
use Qubus\Http\Cookies\CookiesResponse;
use Qubus\Http\Session\SessionService;
class ExpireUserSessionMiddleware implements MiddlewareInterface
{
public const SESSION_ATTRIBUTE = 'EXPIRE_USERSESSION';
public function __construct(protected SessionService $sessionService)
{
}
/**
* @inheritDoc
* @throws TypeException
* @throws Exception
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->sessionService::$options = [
'cookie-name' => 'USERSESSID',
'cookie-lifetime' => 0,
];
$session = $this->sessionService->makeSession($request);
/** @var UserSession $user */
$user = $session->get(type: UserSession::class);
$user
->withToken(token: null)
->withRole(role: null);
$request = $request->withAttribute(self::SESSION_ATTRIBUTE, $session);
$response = $handler->handle($request);
$response = CookiesResponse::set(
response: $response,
setCookieCollection: $this->sessionService->cookie->make(
name: $this->sessionService::$options['cookie-name'],
value: '',
maxAge: 0
)
);
return $this->sessionService->commitSession($response, $session);
}
}