src/EventListener/JWTCreatedListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Core\Users;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  8. class JWTCreatedListener
  9. {
  10.     /**
  11.      * @var RequestStack
  12.      */
  13.     private $requestStack;
  14.     /**
  15.      * @param RequestStack $requestStack
  16.      */
  17.     public function __construct(RequestStack $requestStack)
  18.     {
  19.         $this->requestStack $requestStack;
  20.     }
  21.     /**
  22.      * @param AuthenticationSuccessEvent $event
  23.      */
  24.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  25.     {
  26.         $data $event->getData();
  27.         $user $event->getUser();
  28.         if (!$user instanceof UserInterface) {
  29.             return;
  30.         }
  31.         if ($user instanceof Users) {
  32.             $data['data'] = array(
  33.                 'id'  => $user->getId(),
  34.                 'email' => $user->getEmail(),
  35.                 'roles' => $user->getRoles(),
  36.             );
  37.         }
  38.         $event->setData($data);
  39.     }
  40.     public function onJWTCreated(JWTCreatedEvent $event)
  41.     {
  42.         $user $event->getUser();
  43.         $payload $event->getData();
  44.         // Ajouter des données utilisateur personnalisées
  45.         $payload['userId'] = $user->getId();
  46.         $payload['username'] = $user->getUsername();
  47.         $payload['email'] = $user->getEmail();
  48.         // Autres attributs selon les besoins
  49.         $event->setData($payload);
  50.     }
  51. }