From 61c6a2efe64af747ffcb825e26fdc45d281c37e5 Mon Sep 17 00:00:00 2001 From: Andrius Date: Mon, 14 Dec 2020 00:18:28 +0200 Subject: [PATCH] Update login_link.rst --- security/login_link.rst | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/security/login_link.rst b/security/login_link.rst index b92dd694178..e43edbd7a22 100644 --- a/security/login_link.rst +++ b/security/login_link.rst @@ -654,3 +654,85 @@ user create this POST request (e.g. by clicking a button):: {% endblock %} + +Customizing the Success Handler +............................... + +To customize, how the success handler behaves, create your own ``AuthenticationSuccessHandler``:: + + // src/Security/Authentication/AuthenticationSuccessHandler.php + namespace App\Security\Authentication; + + use Symfony\Component\HttpFoundation\JsonResponse; + use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; + use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; + + class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface + { + public function onAuthenticationSuccess(Request $request, TokenInterface $token): JsonResponse + { + // Example use case: Create API token for Guard Authentication. + $user = $token->getUser(); // Returns string|\Stringable|UserInterface - depends on your implementation. + + $userApiToken = $user->getApiToken(); + + return new JsonResponse(['apiToken' => 'userApiToken']); + } + } + +Modify the configuration and use your handler for the ``success_handler`` key: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/security.yaml + security: + firewalls: + main: + login_link: + check_route: login_check + lifetime: 600 + max_uses: 1 + success_handler: App\Security\Authentication\AuthenticationSuccessHandler + + .. code-block:: xml + + + + + + + + + + + + + .. code-block:: php + + // config/packages/security.php + $container->loadFromExtension('security', [ + 'firewalls' => [ + 'main' => [ + 'login_link' => [ + 'check_route' => 'login_check', + 'lifetime' => 600, + 'max_uses' => 1, + 'success_handler' => 'App\Security\Authentication\AuthenticationSuccessHandler', + ], + ], + ], + ]);