Final Class Yiisoft\User\Login\LoginMiddleware
| Inheritance | Yiisoft\User\Login\LoginMiddleware |
|---|---|
| Implements | Psr\Http\Server\MiddlewareInterface |
LoginMiddleware automatically logs user in if {@see IdentityInterface} instance presents in a request
attribute. It is usually put there by {@see Authentication}.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\User\Login\LoginMiddleware | |
| process() | Yiisoft\User\Login\LoginMiddleware |
Method Details
| public mixed __construct ( Yiisoft\User\CurrentUser $currentUser, \Psr\Log\LoggerInterface $logger ) | ||
| $currentUser | Yiisoft\User\CurrentUser |
The current user instance. |
| $logger | \Psr\Log\LoggerInterface |
The logger instance. |
public function __construct(
private CurrentUser $currentUser,
private LoggerInterface $logger,
) {
}
Before this middleware, there should be {@see \Yiisoft\Auth\Middleware\Authentication} in the middleware stack. It authenticates the user and places {@see \Yiisoft\Auth\IdentityInterface} instance in the corresponding request attribute.
| public process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler ) | ||
| $request | \Psr\Http\Message\ServerRequestInterface | |
| $handler | \Psr\Http\Server\RequestHandlerInterface | |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (!$this->currentUser->isGuest()) {
return $handler->handle($request);
}
/** @var mixed $identity */
$identity = $request->getAttribute(Authentication::class);
if ($identity instanceof IdentityInterface) {
$this->currentUser->login($identity);
} else {
if (is_scalar($identity)) {
$token = is_bool($identity)
? ($identity ? 'true' : 'false')
: ('"' . $identity . '"');
} else {
$token = 'of type ' . get_debug_type($identity);
}
$this->logger->debug(
sprintf('Unable to authenticate user by token %s. Identity not found.', $token)
);
}
return $handler->handle($request);
}
Signup or Login in order to comment.