Final Class Yiisoft\User\Login\LoginMiddleware
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Psr\ |
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\ |
|
| process() | Yiisoft\ |
Method Details
| public mixed __construct ( Yiisoft\ | ||
| $currentUser | Yiisoft\ |
The current user instance. |
| $logger | \ |
The logger instance. |
public function __construct(
private CurrentUser $currentUser,
private LoggerInterface $logger,
) {}
Before this middleware, there should be {@see \
| public process ( \ | ||
| $request | \ |
|
| $handler | \ |
|
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);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.