src/EventListener/AuthenticationFailureListener.php line 14

Open in your IDE?
  1. <?php 
  2. namespace App\EventListener;
  3. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. class AuthenticationFailureListener {
  7.   /**
  8.    * @param AuthenticationFailureEvent $event
  9.    */
  10.   public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event)
  11.   {
  12.     $exception $event->getException();
  13.     $data['error'] = true;
  14.     if ($exception->getMessageKey() === 'unmatched_account') {
  15.       $data = array(
  16.         'message' => 'unmatched_account'
  17.       );
  18.     } elseif ($exception->getMessageKey() === 'email_not_verified') {
  19.       $data = array(
  20.         'message' => 'email_not_verified'
  21.       );
  22.     } elseif ($exception->getMessageKey() === 'account_not_validated') {
  23.       $data = array(
  24.         'message' => 'account_not_validated'
  25.       );
  26.     } else {
  27.       $data = array(
  28.         'error' => true,
  29.         'message' => 'invalid_credentials'
  30.       );
  31.     }
  32.     $data['error'] = true;
  33.     $response = new JWTAuthenticationFailureResponse(''JsonResponse::HTTP_FORBIDDEN);
  34.     $response->setData($data);
  35.       
  36.     $event->setResponse($response);
  37.   }
  38. }