src/EventListener/JwtEventListener.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Model\Error;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\{JWTExpiredEventJWTFailureEventInterfaceJWTNotFoundEvent};
  5. use Lexik\Bundle\JWTAuthenticationBundle\Exception\UserNotFoundException;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. class JwtEventListener
  8. {
  9.     public function onJwtFailure(JWTFailureEventInterface $event)
  10.     {
  11.         if( $event->getException() instanceof UserNotFoundException ) {
  12.             return $event->setResponse(JsonResponse::create(new Error('Unauthorized'), JsonResponse::HTTP_UNAUTHORIZED));
  13.         }
  14.         if( $event instanceof JWTExpiredEvent ) {
  15.             return $event->setResponse(JsonResponse::create(new Error('Expired'), JsonResponse::HTTP_UNAUTHORIZED));
  16.         }
  17.         if( $event instanceof JWTNotFoundEvent ) {
  18.             return $event->setResponse(JsonResponse::create(new Error('Not allowed'), JsonResponse::HTTP_FORBIDDEN));
  19.         }
  20.         return $event->setResponse(JsonResponse::create(new Error('Unauthorized'), JsonResponse::HTTP_UNAUTHORIZED));
  21.     }
  22. }