*/ class AuthenticationListener implements ListenerInterface { /** * @var TokenStorageInterface */ protected $tokenStorage; /** * @var AuthenticationManagerInterface */ protected $authenticationManager; /** * __construct. * * @param TokenStorageInterface $tokenStorage * @param AuthenticationManagerInterface $authenticationManager */ public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager) { $this->tokenStorage = $tokenStorage; $this->authenticationManager = $authenticationManager; } /** * @param GetResponseEvent $event */ 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; } } } }