yiisoft / user

Convenient user identity management and access checking.
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
18 stars 8 forks source link

Remove application layer logic from UserAuth::challenge() #101

Open olegbaturin opened 3 months ago

olegbaturin commented 3 months ago

Метод 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
vjik commented 3 months ago

There are two way to make same thing:

1) Add own failure handler to Authentication middleware.

2) Add code to AuthenticationMethodInterface::challenge().

May be remove AuthenticationMethodInterface::challenge()?