src/EventListener/AuthenticationFailureListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Service\UserManager;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
  6. class AuthenticationFailureListener
  7. {
  8.     private $user_manager;
  9.     public function __construct(UserManager $user_manager)
  10.     {
  11.         $this->user_manager $user_manager;
  12.     }
  13.     public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event){
  14.         $userName $event->getException()->getToken()->getUsername()?$event->getException()->getToken()->getUsername():false;
  15.         if($userName){
  16.             $code $this->user_manager->isUserEnabled($userName);
  17.         }else{
  18.             $code 401;
  19.         }
  20.         $response = new JWTAuthenticationFailureResponse'Bad credentials' $code);
  21.         $event->setResponse($response);
  22.     }
  23. }