src/EventListener/JWTCreatedListener.php line 38

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: rostandnj
  5.  * Date: 15/3/19
  6.  * Time: 5:16 PM
  7.  */
  8. namespace App\EventListener;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. class JWTCreatedListener
  13. {
  14.     /**
  15.      * @var RequestStack
  16.      */
  17.     private $requestStack;
  18.     private $container;
  19.     /**
  20.      * @param RequestStack $requestStack
  21.      */
  22.     public function __construct(RequestStack $requestStack,ContainerInterface $c)
  23.     {
  24.         $this->requestStack $requestStack;
  25.         $this->container $c;
  26.     }
  27.     /**
  28.      * @param JWTCreatedEvent $event
  29.      *
  30.      * @return void
  31.      */
  32.     public function onJWTCreated(JWTCreatedEvent $event)
  33.     {
  34.         $user $event->getUser();
  35.         $payload $event->getData();
  36.         $payload["id"]=$this->container->get("hash_service")->encrypt($user->getId());
  37.         $payload["email"]=$user->getEmail();
  38.         $payload["profile_name"]=$user->getProfileName();
  39.         $event->setData($payload);
  40.         $header        $event->getHeader();
  41.         $header['cty'] = 'JWT';
  42.         $event->setHeader($header);
  43.     }
  44. }