Why isn’t InteractiveLoginEvent not fired with Symfony’s 5.1 new security system?

Tags: , , , ,



With the new Symfony 5.1 security system, I’m not able to fire the InteractiveLoginEvent.

I followed the configuration in the official documentation (here and here) and the system was working perfectly in the former security system.

Below is my security.yaml :

security:
    enable_authenticator_manager: true
    
    encoders:
        AppEntityUser:
            algorithm: auto

        app_user_provider:
            entity:
                class: AppEntityUser
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            lazy: true
            form_login:
                login_path: login
                check_path: login
            entry_point: form_login
            
            guard:
                authenticators:
                    - AppSecurityLoginFormAuthenticator
            logout:
                path: logout

And the UserLocalSuscriber :

namespace AppEventSubscriber;

use SymfonyComponentEventDispatcherEventSubscriberInterface;
use SymfonyComponentHttpFoundationSessionSessionInterface;
use SymfonyComponentSecurityHttpEventInteractiveLoginEvent;
use SymfonyComponentSecurityHttpSecurityEvents;

class UserLocaleSubscriber implements EventSubscriberInterface
{
    private $session;

    public function __construct(SessionInterface $session)
    {
        $this->session = $session;
    }

    public function onInteractiveLogin(InteractiveLoginEvent $event)
    {
        $user = $event->getAuthenticationToken()->getUser();
        if ($user->getLocale() !== null) {
            $this->session->set('_locale', $user->getLocale());
        }
    }

    public static function getSubscribedEvents()
    {
        return [
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
        ];
    }
}

How to configure it correctly? Without it, the user locale is not set properly once the user has been logged in.

Answer

For the Symfony’s new security system, the SecurityEvents::INTERACTIVE_LOGIN has been replaced with SymfonyComponentSecurityHttpEventLoginSuccessEvent.

Change your subscriber to listen for this one:

use SymfonyComponentSecurityHttpEventLoginSuccessEvent;
use SymfonyComponentEventDispatcherEventSubscriberInterface;

class UserLocaleSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            LoginSuccessEvent::class => 'onLoginSuccess',
        ];
    }

    public function onLoginSuccess(LoginSuccessEvent $event): void
    {
        //...
    }

}

These new events are mentioned briefly in the blog with the announcement for the new authenticator system.

The rest of the documentation has not been updated yet, the new auth system will probably become the default on a future Symfony realese, but right now it still is an experimental feature.

enter image description here



Source: stackoverflow