<?php
namespace App\EventListener;
use App\Entity\User;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\SerializerInterface;
class AuthenticationSuccessListener
{
/**
* @var SerializerInterface
*/
private $serializer;
/**
* @var User
*/
private $user;
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* @param AuthenticationSuccessEvent $event
*/
public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
{
$data = $event->getData();
$this->user = $event->getUser();
if (!$this->user instanceof UserInterface) {
return;
}
$userNormalize = $this->serializer->normalize($this->user, null, ['groups' => ["users:read"]]);
if(!empty($userNormalize)) {
$data['user'] = $userNormalize;
}
$event->setData($data);
}
}