src/Listeners/JWTNotFoundListener.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Listeners;
  3. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Exception\ExpiredTokenException;
  6. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Response;
  10. class JWTNotFoundListener
  11. {
  12.     /**
  13.      * @var RequestStack
  14.      */
  15.     private $requestStack;
  16.     /**
  17.      * @var EventDispatcherInterface
  18.      */
  19.     private $dispatcher;
  20.     
  21.     public function __construct(RequestStack $requestStackEventDispatcherInterface $dispatcher)
  22.     {
  23.         $this->requestStack $requestStack;
  24.         $this->dispatcher $dispatcher;
  25.     }
  26.     
  27.     public function onJWTNotFound(JWTNotFoundEvent $event)
  28.     {
  29.         if ($this->requestStack->getCurrentRequest()->cookies->get('REFRESH_TOKEN')) {
  30.             // TODO: dispatch the expired JWT event ??
  31.             // TODO: this needs to be put inside the expired listener and not here
  32.             //            $expiredEvent = new JWTExpiredEvent($event->getException(), $event->getResponse());
  33.             $data = [
  34.                 'status'  => Response::HTTP_UNAUTHORIZED ' Unauthorized',
  35.                 'message' => 'Expired token',
  36.             ];
  37.             $response = new JsonResponse($data401);
  38.             return $event->setResponse($response);
  39.             //            $this->dispatcher->dispatch('lexik_jwt_authentication.on_jwt_expired', $expiredEvent);
  40.             //            return false;
  41.         }
  42.         $data = [
  43.             'status'  => Response::HTTP_FORBIDDEN ' Forbidden',
  44.             'message' => 'Missing token',
  45.         ];
  46.         $response = new JsonResponse($data401);
  47.         return $event->setResponse($response);
  48.     }
  49. }