src/EventSubscriber/JWTSubsciber.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Organization;
  4. use App\Entity\User;
  5. use Doctrine\Common\Collections\Collection;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
  10. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
  11. use Symfony\Component\HttpFoundation\Response;
  12. class JWTSubsciber
  13. {
  14.     public function onJWTCreated(JWTCreatedEvent $event)
  15.     {
  16.         $data $event->getData();
  17.         $user $event->getUser();
  18.         if (!$user instanceof User) {
  19.             return;
  20.         }
  21.         /** @var Collection $organizations */
  22.         $organizations $user->getOrganizations();
  23.         if ($organizations->count() > 0) {
  24.             /** @var Organization $organization */
  25.             $organization $organizations->first();
  26.             $data['organization_id'] = $organization->getId();
  27.             $event->setData($data);
  28.         }
  29.     }
  30.     public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event)
  31.     {
  32.         $data = [
  33.             'code' => 'authenticationFailure',
  34.             'detail' => 'Bad credentials, please verify that your username/password are correctly set',
  35.             'status' => Response::HTTP_UNPROCESSABLE_ENTITY,
  36.             'title' => 'Unauthorized',
  37.             'errors' => [],
  38.         ];
  39.         $response = new Response(
  40.             json_encode($data),
  41.             Response::HTTP_UNPROCESSABLE_ENTITY
  42.         );
  43.         $response->headers->set('Content-Type''application/json');
  44.         $event->setResponse($response);
  45.     }
  46.     public function onJWTInvalid(JWTInvalidEvent $event)
  47.     {
  48.         $data = [
  49.             'code' => 'invalidJWT',
  50.             'detail' => 'Your token is invalid, please login again to get a new one',
  51.             'status' => Response::HTTP_UNAUTHORIZED,
  52.             'title' => 'Forbidden',
  53.             'errors' => [],
  54.         ];
  55.         $response = new Response(
  56.             json_encode($data),
  57.             Response::HTTP_UNAUTHORIZED
  58.         );
  59.         $response->headers->set('Content-Type''application/json');
  60.         $event->setResponse($response);
  61.     }
  62.     public function onJWTNotFound(JWTNotFoundEvent $event)
  63.     {
  64.         $data = [
  65.             'code' => 'missingJWT',
  66.             'detail' => 'Missing token',
  67.             'status' => Response::HTTP_UNAUTHORIZED,
  68.             'title' => 'Forbidden',
  69.             'errors' => [],
  70.         ];
  71.         $response = new Response(
  72.             json_encode($data),
  73.             Response::HTTP_UNAUTHORIZED
  74.         );
  75.         $response->headers->set('Content-Type''application/json');
  76.         $event->setResponse($response);
  77.     }
  78.     public function onJWTExpired(JWTExpiredEvent $event)
  79.     {
  80.         $data = [
  81.             'code' => 'expiredJWT',
  82.             'detail' => 'Your token is expired, please renew it',
  83.             'status' => Response::HTTP_UNAUTHORIZED,
  84.             'title' => 'Forbidden',
  85.             'errors' => [],
  86.         ];
  87.         $response = new Response(
  88.             json_encode($data),
  89.             Response::HTTP_UNAUTHORIZED
  90.         );
  91.         $response->headers->set('Content-Type''application/json');
  92.         $event->setResponse($response);
  93.     }
  94. }