*/ class AuthenticationListener implements ListenerInterface { protected $tokenStorage; protected $authenticationManager; public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager) { $this->tokenStorage = $tokenStorage; $this->authenticationManager = $authenticationManager; } public function handle(GetResponseEvent $event) { $request = $event->getRequest(); $username = $request->get('_username'); $password = $request->get('_password'); if (!empty($username)) { $token = new UsernamePasswordToken($username, $password, 'default'); try { $authToken = $this->authenticationManager->authenticate($token); $this->tokenStorage->setToken($token); return; } catch (AuthenticationException $failed) { $this->tokenStorage->setToken(null); return; } } } }