<?php
namespace App\EventSubscriber;
use App\Entity\Profile;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTDecodedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class JWTSubscriber implements EventSubscriberInterface
{
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public static function getSubscribedEvents()
{
return array(
Events::JWT_CREATED => 'onJwtCreated',
Events::JWT_DECODED => 'onJwtDecoded',
Events::JWT_AUTHENTICATED => 'onJwtAuthenticated',
);
}
public function onJwtCreated(JWTCreatedEvent $event)
{
$payload = $event->getData();
$payload['profile'] = $event->getUser()->getProfile()->getId();
$event->setData($payload);
}
public function onJwtDecoded(JWTDecodedEvent $event)
{
$payload = $event->getPayload();
$payload['profile'] = $this->em->getRepository(Profile::class)->find($payload['profile']);
$event->setPayload($payload);
}
public function onJwtAuthenticated(JWTAuthenticatedEvent $event)
{
$payload = $event->getPayload();
$event->setPayload($payload);
}
}