src/EventListener/JWTCreatedListener.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
  10. use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
  12. class JWTCreatedListener
  13. {
  14.     public function __construct(
  15.         private RequestStack $requestStack
  16.         private EntityManagerInterface $entityManager
  17.         private ContainerInterface $container
  18.     ) { }
  19.   public function onAuthenticationSuccessEvent(AuthenticationSuccessEvent $event)
  20.   {
  21.   }
  22.   /**
  23.    * @param JWTCreatedEvent $event
  24.    *
  25.    * @return void
  26.    */
  27.   public function onJWTCreated(JWTCreatedEvent $event)
  28.   {
  29.     $payload $event->getData();
  30.     if ($user $event->getUser()) {
  31.         $payload['id'] = $user->getId();
  32.         $payload['guest'] = $user->getGuest();
  33.         $payload['uuid'] = $user->getUuid();
  34.     } else {
  35.         $payload['guest'] = true;
  36.         $payload['uuid'] = $user->getUuid();
  37.     }
  38.     $this->customExp($payload);
  39.     
  40.     $event->setData($payload);
  41.   }
  42.     private function customExp(&$payload)
  43.     {
  44.         $request = new Request;
  45.         if (!$request->getContent()) {
  46.             return ;
  47.         }
  48.         if (!$exp $request->toArray()['exp'] ?? null) {
  49.             return ;
  50.         }
  51.         $token_ttl = (int)$this->container->getParameter('lexik_jwt_authentication.token_ttl');
  52.         $expiration = new \DateTime();
  53.     
  54.         $min max(0min($exp$token_ttl));
  55.         $expiration->modify($min ' min');
  56.     
  57.         $payload['exp'] = $expiration->getTimestamp();
  58.     }
  59.   
  60.   /**
  61.    * @param JWTExpiredEvent $event
  62.    *
  63.    * @return void
  64.    */
  65.   public function onJWTExpired(JWTExpiredEvent $event)
  66.   {
  67.       $response = new JWTAuthenticationFailureResponse('Your token is invalid, please login again to get a new one'403);
  68.       $event->setResponse($response);
  69.   }
  70.   /**
  71.    * @param JWTInvalidEvent $event
  72.    *
  73.    * @return void
  74.    */
  75.   public function onJWTInvalid(JWTInvalidEvent $event)
  76.   {
  77.       $response = new JWTAuthenticationFailureResponse('Your token is invalid, please login again to get a new one'403);
  78.       $event->setResponse($response);
  79.   }
  80.   /**
  81.    * @param AuthenticationFailureEvent $event
  82.    *
  83.    * @return void
  84.    */
  85.   public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event)
  86.   {
  87.       $response = new JWTAuthenticationFailureResponse('Bad credentials, please verify that your login/password are correctly set'401);
  88.       $event->setResponse($response);
  89.   }
  90. }