src/EventListener/JWTAuthenticatedListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  4. use Namshi\JOSE\JWS;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. class JWTAuthenticatedListener
  7. {
  8.     private string $tokenTTL;
  9.     /**
  10.      * JWTAuthenticatedListener constructor.
  11.      *
  12.      * @param string $tokenTTL
  13.      */
  14.     public function __construct($tokenTTL)
  15.     {
  16.         $this->tokenTTL $tokenTTL;
  17.     }
  18.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  19.     {
  20.         $data $event->getData();
  21.         $user $event->getUser();
  22.         if (!$user instanceof UserInterface) {
  23.             return;
  24.         }
  25.         $token JWS::load($data['token']);
  26.         $data['tokenDuration'] = (int) $this->tokenTTL;
  27.         $data['tokenExpirationDate'] = date(\DateTimeInterface::RFC3339$token->getPayload()['exp']);
  28.         $event->setData($data);
  29.     }
  30. }