<?php
declare(strict_types=1);
namespace App\EventListener;
use JMS\Serializer\SerializerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
class AttachUserDataOnSuccessListener
{
/**
* @var \JMS\Serializer\Serializer
*/
protected $normalizer;
public function __construct(SerializerInterface $normalizer)
{
$this->normalizer = $normalizer;
}
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
$token = $event->getData();
$user = $event->getUser();
$event->setData([
'token' => $token,
'user' => $this->normalizer->toArray($user)
]);
}
}