<?php
namespace App\EventListener;
use App\Entity\Core\Users;
use Symfony\Component\HttpFoundation\RequestStack;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
class JWTCreatedListener
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @param RequestStack $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
/**
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
$user = $event->getUser();
if (!$user instanceof UserInterface) {
return;
}
if ($user instanceof Users) {
$data['data'] = array(
'id' => $user->getId(),
'email' => $user->getEmail(),
'roles' => $user->getRoles(),
);
}
$event->setData($data);
}
public function onJWTCreated(JWTCreatedEvent $event)
{
$user = $event->getUser();
$payload = $event->getData();
// Ajouter des données utilisateur personnalisées
$payload['userId'] = $user->getId();
$payload['username'] = $user->getUsername();
$payload['email'] = $user->getEmail();
// Autres attributs selon les besoins
$event->setData($payload);
}
}