src/EventSubscriber/JWTSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Profile;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
  6. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
  7. use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
  8. use Lexik\Bundle\JWTAuthenticationBundle\Events;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class JWTSubscriber implements EventSubscriberInterface
  11. {
  12.     private $em;
  13.     public function __construct(EntityManagerInterface $em)
  14.     {
  15.         $this->em $em;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return array(
  20.             Events::JWT_CREATED => 'onJwtCreated',
  21.             Events::JWT_DECODED => 'onJwtDecoded',
  22.             Events::JWT_AUTHENTICATED => 'onJwtAuthenticated',
  23.         );
  24.     }
  25.     public function onJwtCreated(JWTCreatedEvent $event)
  26.     {
  27.         $payload $event->getData();
  28.         $payload['profile'] = $event->getUser()->getProfile()->getId();
  29.         $event->setData($payload);
  30.     }
  31.     public function onJwtDecoded(JWTDecodedEvent $event)
  32.     {
  33.         $payload $event->getPayload();
  34.         $payload['profile'] = $this->em->getRepository(Profile::class)->find($payload['profile']);
  35.         $event->setPayload($payload);
  36.     }
  37.     public function onJwtAuthenticated(JWTAuthenticatedEvent $event)
  38.     {
  39.         $payload $event->getPayload();
  40.         $event->setPayload($payload);
  41.     }
  42. }