Final Class Yiisoft\User\Login\Cookie\CookieLogin
| Inheritance | Yiisoft\User\Login\Cookie\CookieLogin |
|---|
The service is used to send or remove auto-login cookie.
See also:
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\User\Login\Cookie\CookieLogin | |
| addCookie() | Adds auto-login cookie to response so the user is logged in automatically based on cookie even if session is expired. | Yiisoft\User\Login\Cookie\CookieLogin |
| expireCookie() | Expires auto-login cookie so user is not logged in automatically anymore. | Yiisoft\User\Login\Cookie\CookieLogin |
| getCookieName() | Returns the auto-login cookie name. | Yiisoft\User\Login\Cookie\CookieLogin |
| withCookieName() | Returns a new instance with the specified auto-login cookie name. | Yiisoft\User\Login\Cookie\CookieLogin |
Method Details
| public mixed __construct ( DateInterval|null $duration = null ) | ||
| $duration | DateInterval|null |
Interval until the auto-login cookie expires. If it isn't set it means the auto-login cookie is session cookie that expires when browser is closed. |
public function __construct(private ?DateInterval $duration = null)
{
}
Adds auto-login cookie to response so the user is logged in automatically based on cookie even if session is expired.
| public \Psr\Http\Message\ResponseInterface addCookie ( Yiisoft\User\Login\Cookie\CookieLoginIdentityInterface $identity, \Psr\Http\Message\ResponseInterface $response, DateInterval|false|null $duration = false ) | ||
| $identity | Yiisoft\User\Login\Cookie\CookieLoginIdentityInterface |
The cookie login identity instance. |
| $response | \Psr\Http\Message\ResponseInterface |
Response for adding auto-login cookie. |
| $duration | DateInterval|false|null |
Interval until the auto-login cookie expires. If it is null it means the auto-login cookie is session cookie that expires when browser is closed. If it is false (by default) will be used default value of duration. |
| return | \Psr\Http\Message\ResponseInterface |
Response with added auto-login cookie. |
|---|---|---|
| throws | JsonException |
If an error occurs during JSON encoding of the cookie value. |
public function addCookie(
CookieLoginIdentityInterface $identity,
ResponseInterface $response,
DateInterval|null|false $duration = false,
): ResponseInterface {
$duration = $duration === false ? $this->duration : $duration;
$data = [$identity->getId(), $identity->getCookieLoginKey()];
if ($duration !== null) {
$expires = (new DateTimeImmutable())->add($duration);
$data[] = $expires->getTimestamp();
} else {
$expires = null;
$data[] = 0;
}
$cookieValue = json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
return (new Cookie(name: $this->cookieName, value: $cookieValue, expires: $expires))->addToResponse($response);
}
Expires auto-login cookie so user is not logged in automatically anymore.
| public \Psr\Http\Message\ResponseInterface expireCookie ( \Psr\Http\Message\ResponseInterface $response ) | ||
| $response | \Psr\Http\Message\ResponseInterface |
Response for adding auto-login cookie. |
| return | \Psr\Http\Message\ResponseInterface |
Response with added auto-login cookie. |
|---|---|---|
public function expireCookie(ResponseInterface $response): ResponseInterface
{
return (new Cookie($this->cookieName))
->expire()
->addToResponse($response);
}
Returns the auto-login cookie name.
| public string getCookieName ( ) | ||
| return | string |
The auto-login cookie name. |
|---|---|---|
public function getCookieName(): string
{
return $this->cookieName;
}
Returns a new instance with the specified auto-login cookie name.
| public self withCookieName ( string $name ) | ||
| $name | string |
The auto-login cookie name. |
public function withCookieName(string $name): self
{
$new = clone $this;
$new->cookieName = $name;
return $new;
}
Signup or Login in order to comment.