Final Class Yiisoft\User\Login\Cookie\CookieLoginMiddleware
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
CookieLoginMiddleware automatically logs user in based on cookie.
The auto-login cookie value must be protected against tampering: either configure a signature key for
{@see \
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| process() | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $currentUser | Yiisoft\ |
The current user instance. |
| $identityRepository | \ |
The identity repository instance. |
| $logger | \ |
The logger instance. |
| $cookieLogin | Yiisoft\ |
The cookie login instance. |
| $forceAddCookie | boolean |
Whether to force add a cookie. |
public function __construct(
private readonly CurrentUser $currentUser,
private readonly IdentityRepositoryInterface $identityRepository,
private readonly LoggerInterface $logger,
private readonly CookieLogin $cookieLogin,
private readonly bool $forceAddCookie = false,
) {}
| public process ( \ | ||
| $request | \ |
|
| $handler | \ |
|
| throws | JsonException |
If an error occurs when JSON encoding the cookie value while adding the cookie file. |
|---|---|---|
| throws | RuntimeException |
If during authentication, the identity repository {@see \ |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->currentUser->isGuest()) {
$this->authenticateUserByCookieFromRequest($request);
}
$guestBeforeHandle = $this->currentUser->isGuest();
$response = $handler->handle($request);
$guestAfterHandle = $this->currentUser->isGuest();
if ($this->forceAddCookie && $guestBeforeHandle && !$guestAfterHandle) {
$identity = $this->currentUser->getIdentity();
if ($identity instanceof CookieLoginIdentityInterface) {
$response = $this->cookieLogin->addCookie($identity, $response);
}
}
if (!$guestBeforeHandle && $guestAfterHandle) {
$response = $this->cookieLogin->expireCookie($response);
}
return $response;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.