<?php
declare(strict_types=1);
namespace App\EventSubscriber\Auth;
use App\Entity\Creator;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CreatorJwtClaimsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
// Lexik dispatches with Events::JWT_CREATED, not JWTCreatedEvent::class — see JWTManager::generateJwtStringAndDispatchEvents.
return [
Events::JWT_CREATED => 'onJwtCreated',
];
}
public function onJwtCreated(JWTCreatedEvent $event): void
{
$user = $event->getUser();
if (!$user instanceof Creator) {
return;
}
$data = $event->getData();
$data['user_id'] = $user->getOwningUserId();
$event->setData($data);
}
}