src/EventListener/CustomJWTEventListener.php line 20

  1. <?php
  2. namespace App\EventListener;
  3. use App\Service\BlacklistService;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  8. class CustomJWTEventListener
  9. {
  10.     private $blacklistService;
  11.     public function __construct(BlacklistService $blacklistService)
  12.     {
  13.         $this->blacklistService $blacklistService;
  14.     }
  15.     public function onKernelRequest(RequestEvent $event)
  16.     {
  17.         try {
  18.             $request $event->getRequest();
  19.             $credentials $request->headers->get('Authorization');
  20.             $token str_replace('Bearer '''$credentials);
  21.     
  22.             if ($this->blacklistService->isBlacklisted($token)) {
  23.                 throw new NotFoundHttpException('This token has been blacklisted.');
  24.             }
  25.         } catch (NotFoundHttpException $exception) {
  26.             throw new NotFoundHttpException($exception->getMessage());
  27.         }
  28.     }
  29. }