Remove application layer logic from UserAuth::challenge()
Метод UserAuth::challenge() должен возвращать переданный в него $response. Приложение должно само подготовить Response в соответствии с его логикой обработки неаутентифицированного пользователя в обрабтчике RequestHandlerInterface который конфигурируется в мидваре Authentication пакета yiisoft/auth через свойство failureHandler.
Логика с 302 редиректом подходит для обычного приложения с рендерингом ответа на бэкэнде, но не подходит для SPA приложения.
Пример обработчика ответа
final class AuthRequestErrorHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->responseFactory
->createResponse(Status::FOUND)
->withHeader('Location', $this->authUrl);
}
}
di
Authentication::class => [
'class' => Authentication::class,
'__construct()' => [
'authenticationFailureHandler' => Reference::to(AuthRequestErrorHandler::class),
],
],
Additional info
| Q | A |
|---|---|
| Version | 2.2.0 |
There are two way to make same thing:
-
Add own failure handler to
Authenticationmiddleware. -
Add code to
AuthenticationMethodInterface::challenge().
May be remove AuthenticationMethodInterface::challenge()?