src/EventSubscriber/JwtCreatedSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. /**
  7.  * @todo VĂ©rifier si on conserve le subscriber ou le listener
  8.  */
  9. class JwtCreatedSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             JWTCreatedEvent::class => 'onJWTCreated',
  15.         ];
  16.     }
  17.     public function onJWTCreated(JWTCreatedEvent $event): void
  18.     {
  19.         $data $event->getData();
  20.         $user $event->getUser();
  21.         if (!$user instanceof UserInterface) {
  22.             return;
  23.         }
  24.         $data['email'] = $user->getEmail();
  25.         $data['roles'] = $user->getRoles();
  26.         $data['testkey'] = 'testvalue';
  27.         $event->setData($data);
  28.         file_put_contents('test.txt''text');
  29.     }
  30. }