src/Service/JWTCreatedListener.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  5. class JWTCreatedListener
  6. {
  7.     /**
  8.      * @var TokenStorageInterface
  9.      */
  10.     private $tokenStorage;
  11.     /**
  12.      * @param TokenStorageInterface $tokenStorage
  13.      */
  14.     public function __constructTokenStorageInterface $tokenStorage)
  15.     {
  16.         $this->tokenStorage $tokenStorage;
  17.     }
  18.     /**
  19.      * Adds additional data to the generated JWT
  20.      *
  21.      * @param JWTCreatedEvent $event
  22.      *
  23.      * @return void
  24.      */
  25.     public function onJWTCreated(JWTCreatedEvent $event)
  26.     {
  27.         
  28.         /** @var $user \App\Application\Sonata\UserBundle\Entity\User */
  29.         $user $event->getUser();
  30.         // add new data
  31.         $payload['userId'] = $user->getId();
  32.         $payload['username'] = $user->getUsername();
  33.         
  34.         $event->setData($payload);
  35.     }
  36. }