Skip to content

Commit 61c6a2e

Browse files
ulinskaswouterj
authored andcommitted
Update login_link.rst
1 parent 3fac19f commit 61c6a2e

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

security/login_link.rst

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,85 @@ user create this POST request (e.g. by clicking a button)::
654654
<button type="submit">Continue</button>
655655
</form>
656656
{% endblock %}
657+
658+
Customizing the Success Handler
659+
...............................
660+
661+
To customize, how the success handler behaves, create your own ``AuthenticationSuccessHandler``::
662+
663+
// src/Security/Authentication/AuthenticationSuccessHandler.php
664+
namespace App\Security\Authentication;
665+
666+
use Symfony\Component\HttpFoundation\JsonResponse;
667+
use Symfony\Component\HttpFoundation\Request;
668+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
669+
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
670+
671+
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
672+
{
673+
public function onAuthenticationSuccess(Request $request, TokenInterface $token): JsonResponse
674+
{
675+
// Example use case: Create API token for Guard Authentication.
676+
$user = $token->getUser(); // Returns string|\Stringable|UserInterface - depends on your implementation.
677+
678+
$userApiToken = $user->getApiToken();
679+
680+
return new JsonResponse(['apiToken' => 'userApiToken']);
681+
}
682+
}
683+
684+
Modify the configuration and use your handler for the ``success_handler`` key:
685+
686+
.. configuration-block::
687+
688+
.. code-block:: yaml
689+
690+
# config/packages/security.yaml
691+
security:
692+
firewalls:
693+
main:
694+
login_link:
695+
check_route: login_check
696+
lifetime: 600
697+
max_uses: 1
698+
success_handler: App\Security\Authentication\AuthenticationSuccessHandler
699+
700+
.. code-block:: xml
701+
702+
<!-- config/packages/security.xml -->
703+
<?xml version="1.0" encoding="UTF-8"?>
704+
<srv:container xmlns="http://symfony.com/schema/dic/security"
705+
xmlns:srv="http://symfony.com/schema/dic/services"
706+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
707+
xsi:schemaLocation="http://symfony.com/schema/dic/services
708+
https://symfony.com/schema/dic/services/services-1.0.xsd
709+
http://symfony.com/schema/dic/security
710+
https://symfony.com/schema/dic/security/security-1.0.xsd">
711+
712+
<config>
713+
<firewall name="main">
714+
<login-link check-route="login_check"
715+
check-post-only="true"
716+
max-uses="1"
717+
lifetime="600"
718+
success_handler="App\Security\Authentication\AuthenticationSuccessHandler"
719+
/>
720+
</firewall>
721+
</config>
722+
</srv:container>
723+
724+
.. code-block:: php
725+
726+
// config/packages/security.php
727+
$container->loadFromExtension('security', [
728+
'firewalls' => [
729+
'main' => [
730+
'login_link' => [
731+
'check_route' => 'login_check',
732+
'lifetime' => 600,
733+
'max_uses' => 1,
734+
'success_handler' => 'App\Security\Authentication\AuthenticationSuccessHandler',
735+
],
736+
],
737+
],
738+
]);

0 commit comments

Comments
 (0)