user icon indicating copy to clipboard operation
user copied to clipboard

Remove application layer logic from UserAuth::challenge()

Open olegbaturin opened this issue 1 year ago • 1 comments

Метод 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

olegbaturin avatar May 16 '24 13:05 olegbaturin

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()?

vjik avatar May 17 '24 07:05 vjik