src/EventSubscriber/Auth/CreatorJwtClaimsSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\Auth;
  4. use App\Entity\Creator;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Events;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class CreatorJwtClaimsSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         // Lexik dispatches with Events::JWT_CREATED, not JWTCreatedEvent::class — see JWTManager::generateJwtStringAndDispatchEvents.
  13.         return [
  14.             Events::JWT_CREATED => 'onJwtCreated',
  15.         ];
  16.     }
  17.     public function onJwtCreated(JWTCreatedEvent $event): void
  18.     {
  19.         $user $event->getUser();
  20.         if (!$user instanceof Creator) {
  21.             return;
  22.         }
  23.         $data $event->getData();
  24.         $data['user_id'] = $user->getOwningUserId();
  25.         $event->setData($data);
  26.     }
  27. }