src/EventListener/AuthenticationSuccessListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\User;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Serializer\SerializerInterface;
  7. class AuthenticationSuccessListener
  8. {
  9.     /**
  10.      * @var SerializerInterface
  11.      */
  12.     private $serializer;
  13.     /**
  14.      * @var User
  15.      */
  16.     private $user;
  17.     public function __construct(SerializerInterface $serializer)
  18.     {
  19.         $this->serializer $serializer;
  20.     }
  21.     /**
  22.      * @param AuthenticationSuccessEvent $event
  23.      */
  24.     public function onAuthenticationSuccessResponse(AuthenticationSuccessEvent $event)
  25.     {
  26.         $data $event->getData();
  27.         $this->user $event->getUser();
  28.         if (!$this->user instanceof UserInterface) {
  29.             return;
  30.         }
  31.         $userNormalize $this->serializer->normalize($this->usernull, ['groups' => ["users:read"]]);
  32.         if(!empty($userNormalize)) {
  33.             $data['user'] = $userNormalize;
  34.         }
  35.         $event->setData($data);
  36.     }
  37. }