@@ -656,9 +656,12 @@ user create this POST request (e.g. by clicking a button)::
656
656
{% endblock %}
657
657
658
658
Customizing the Success Handler
659
- ...............................
659
+ -------------------------------
660
660
661
- To customize, how the success handler behaves, create your own ``AuthenticationSuccessHandler ``::
661
+ Sometimes, the default success handling does not fit your use-case (e.g.
662
+ when you need to generate and return an API key). To customize how the
663
+ success handler behaves, create your own
664
+ :class: `Symfony\C omponent\S ecurity\H ttp\A uthentication\A uthenticationSuccessHandlerInterface `::
662
665
663
666
// src/Security/Authentication/AuthenticationSuccessHandler.php
664
667
namespace App\Security\Authentication;
@@ -672,16 +675,14 @@ To customize, how the success handler behaves, create your own ``AuthenticationS
672
675
{
673
676
public function onAuthenticationSuccess(Request $request, TokenInterface $token): JsonResponse
674
677
{
675
- // Example use case: Create API token for Guard Authentication.
676
- $user = $token->getUser(); // Returns string|\Stringable|UserInterface - depends on your implementation.
677
-
678
+ $user = $token->getUser();
678
679
$userApiToken = $user->getApiToken();
679
680
680
681
return new JsonResponse(['apiToken' => 'userApiToken']);
681
682
}
682
683
}
683
684
684
- Modify the configuration and use your handler for the ``success_handler `` key :
685
+ Then, configure this service ID as the ``success_handler ``:
685
686
686
687
.. configuration-block ::
687
688
@@ -715,7 +716,7 @@ Modify the configuration and use your handler for the ``success_handler`` key:
715
716
check-post-only =" true"
716
717
max-uses =" 1"
717
718
lifetime =" 600"
718
- success_handler =" App\Security\Authentication\AuthenticationSuccessHandler"
719
+ success-handler =" App\Security\Authentication\AuthenticationSuccessHandler"
719
720
/>
720
721
</firewall >
721
722
</config >
@@ -724,14 +725,16 @@ Modify the configuration and use your handler for the ``success_handler`` key:
724
725
.. code-block :: php
725
726
726
727
// config/packages/security.php
728
+ use App\Security\Authentication\AuthenticationSuccessHandler;
729
+
727
730
$container->loadFromExtension('security', [
728
731
'firewalls' => [
729
732
'main' => [
730
733
'login_link' => [
731
734
'check_route' => 'login_check',
732
735
'lifetime' => 600,
733
736
'max_uses' => 1,
734
- 'success_handler' => 'App\Security\Authentication\ AuthenticationSuccessHandler' ,
737
+ 'success_handler' => AuthenticationSuccessHandler::class ,
735
738
],
736
739
],
737
740
],
0 commit comments