src/EventListener/JWTCreatedListener.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use App\Repository\UserRepository;
  6. class JWTCreatedListener{
  7.   
  8.     private $requestStack;
  9.     /**
  10.      * @param RequestStack $requestStack
  11.      */
  12.     public function __construct(UserRepository $userRepoRequestStack $requestStack)
  13.     {
  14.         $this->userRepo $userRepo;
  15.         $this->requestStack $requestStack;
  16.     }
  17.     public function onJWTCreated(JWTCreatedEvent $event)
  18.     {
  19.         $request $this->requestStack->getCurrentRequest();
  20.         $payload $event->getData();
  21.         $user $this->userRepo->findOneByUserName($payload['username']);
  22.         $payload['id'] = $user->getId();
  23.         $payload['email'] = $user->getEmail();
  24.         $payload['name'] = $user->getName();
  25.         $payload['lastName'] = $user->getLastName();
  26.         $payload['occupation'] = $user->getOccupation();
  27.         $payload['phoneExternal'] = $user->getPhoneExternal();
  28.         $payload['phoneExtension'] = $user->getphoneExtension();
  29.         $payload['googleScholarLink'] = $user->getGoogleScholarLink();
  30.         $payload['researchGateLink'] = $user->getresearchGateLink();
  31.         $payload['customRoles'] = $user->getCustomRoles();
  32.     
  33.         $event->setData($payload);
  34.         
  35.         $header        $event->getHeader();
  36.         $header['cty'] = 'JWT';
  37.     
  38.         $event->setHeader($header);
  39.     }
  40. }