Insights

How To Create An Authentication Listener In Symfony2

If you want to keep a track of the user's failed login attempts so that the account can be locked or you want to set the last login date for a user on his or her successful login then Symfony2 will enable several events for authentication. The security.authentication.failure event will be dispatched on failed login and the security.interactive_login event will be dispatched on successful authentication. The Symfony2 will allow the Symfony developers to create an authentication listener class that will subscribe to those events so that the code can be executed when the events are dispatched.

// AuthenticationListener.php

namespace pathtoyourclass;
 
use SymfonyComponentSecurityCoreEventAuthenticationFailureEvent;
use SymfonyComponentSecurityHttpEventInteractiveLoginEvent;
 
class AuthenticationListener
{
    /**
     * onAuthenticationFailure
     *
     * @author     Joe Sexton <[email protected]>
     * @param     AuthenticationFailureEvent $event
     */
    public function onAuthenticationFailure( AuthenticationFailureEvent $event )
    {
        // executes on failed login
    }
 
    /**
     * onAuthenticationSuccess
     *
     * @author     Joe Sexton <[email protected]>
     * @param     InteractiveLoginEvent $event
     */
    public function onAuthenticationSuccess( InteractiveLoginEvent $event )
    {
        // executes on successful login
    }
}

The class includes two methods. One that subscribes to an authentication failure event and one that subscribes to an interactive login event. The next step consists of subscribing the class methods to the events. This can be achieved in different ways first is in your bundle's Resources/config/services.yml file like:

# Resources/config/services.yml
    
    # authentication failure event listener
    acme.security.authentication_failure_event_listener:
        class: pathtoyourclassAuthenticationListener
        tags:
            - { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }
 
    # authentication success event listener
    acme.security.interactive_login_listener:
        class: pathtoyourclassAuthenticationListener
        tags:
            - { name: kernel.event_listener, event: security.interactive_login, method: onAuthenticationSuccess }

We have successfully subscribed the two methods to their respective events. The other way you can subscribe to the events is by adding a getSubscribedEvents method to the class such as:

// AuthenticationListener.php

namespace pathtoyourclass;
 
use SymfonyComponentSecurityCoreAuthenticationEvents;
use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentSecurityCoreEventAuthenticationFailureEvent;
use SymfonyComponentSecurityHttpEventInteractiveLoginEvent;
 
class AuthenticationListener implements EventSubscriberInterface
{
    /**
     * getSubscribedEvents
     *
     * @author     Joe Sexton <[email protected]>
     * @return     array
     */
    public static function getSubscribedEvents()
    {
        return array(
            AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
            AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
        );
    }
 
    /**
     * onAuthenticationFailure
     *
     * @author     Joe Sexton <[email protected]>
     * @param     AuthenticationFailureEvent $event
     */
    public function onAuthenticationFailure( AuthenticationFailureEvent $event )
    {
        // executes on failed login
    }
 
    /**
     * onAuthenticationSuccess
     *
     * @author     Joe Sexton <[email protected]>
     * @param     InteractiveLoginEvent $event
     */
    public function onAuthenticationSuccess( InteractiveLoginEvent $event )
    {
        // executes on successful login
    }
}

The services file would appear a little different and will look like this:
# Resources/config/services.yml
    
    # authentication failure event listener
    acme.security.authentication_event_listener:
        class: pathtoyourclassAuthenticationListener
        tags:
            - { name: kernel.event_subscriber }

These are two different ways you can subscribe to Symfony's login events. So, what keeps you waiting? Contact us today for Symfony Web Development!

Banner
In search for strategic sessions?
Let us understand your business thoroughly and help you strategies your digital product..
Book a session