Skip to content
1 change: 1 addition & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
* :doc:`/cookbook/security/remember_me`
* :doc:`/cookbook/security/impersonating_user`
* :doc:`/cookbook/security/form_login`
* :doc:`/cookbook/security/login_handlers`
* :doc:`/cookbook/security/custom_provider`
* :doc:`/cookbook/security/custom_password_authenticator`
* :doc:`/cookbook/security/api_key_authentication`
Expand Down
1 change: 1 addition & 0 deletions cookbook/security/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Authentication (Identifying/Logging in the User)
remember_me
impersonating_user
form_login
login_handlers
custom_provider
custom_password_authenticator
api_key_authentication
Expand Down
95 changes: 95 additions & 0 deletions cookbook/security/login_handlers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
.. index::
single: Security; Login
single: Security; Logout
single: Security; Handler

How to Customize the Success and Failure Login Handlers
=======================================================

After the users successfully log in in your application, they are redirected to
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, log in to your application

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thanks.

the proper URL according to the security configuration options. This is done in
the :class:`Symfony\\Component\\Security\\Http\\Authentication\\DefaultAuthenticationSuccessHandler`
class and Symfony defines a similar class called ``DefaultAuthenticationFailureHandler``
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we link to this DefaultAuthenticationFailureHandler class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

to handle the login failures.

This article explains **how to define your own login handlers** to execute
custom logic after the user has logged in successfully or failed to do that.

Creating a Success Login Handler
--------------------------------

First, create a class that implements :class:`Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationSuccessHandlerInterface`
and add your own logic::

namespace AppBundle\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;

class SuccesfulLoginHandler implements AuthenticationSuccessHandlerInterface
{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
// do something...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just use // ... here


// you can inherit from the DefaultAuthenticationSuccessHandler
// class to reuse the logic that decides the URL to redirect to
return new RedirectResponse(...);
}
}

Then, define a new service for this login handler:

.. code-block:: yaml

# app/config/services.yml
services:
app.security.success_login:
class: AppBundle\Security\SuccesfulLoginHandler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the other config formats too


Lastly, add a new ``success_handler`` option under the configuration of the
firewalls where this handler will be enabled and pass the ``id`` of the service
as its value:

.. code-block:: yaml

# app/config/security.yml
firewalls:
main:
pattern: ^/
form_login:
success_handler: app.security.successful_login

Creating a Failure Login Handler
--------------------------------

The steps to follow are identical to the ones explained in the previous section.
First, define your own logic in a class that implements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] implements the [...]

:class:`Symfony\\Component\\Security\\Http\\Authentication\\AuthenticationFailureHandlerInterface`
and create a new service for it. Then, add the ``failure_handler`` configuration
option in your firewall:

.. code-block:: yaml

# app/config/security.yml
firewalls:
main:
pattern: ^/
form_login:
failure_handler: app.security.failure_login
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


When Should Login Handlers Be Used?
-----------------------------------

These security handlers are closely related to the ``security.authentication.success``
and ``security.authentication.failure`` events, but Symfony also defines an event
called ``security.interactive_login`` that lets you customize the behavior of
the login process.

The success/failure handlers should be used when you need to change the login
behavior on success/failure by changing the returned ``Response`` object.

The listener hooked into ``security.interactive_login`` should be used when you
need to execute some code on login success/failure but without altering the
``Response`` object being sent. For example, to store in a Redis cache the number
of failed login attempts to protect against brute-force attacks.