<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Repository\UserRepository;
class JWTCreatedListener{
private $requestStack;
/**
* @param RequestStack $requestStack
*/
public function __construct(UserRepository $userRepo, RequestStack $requestStack)
{
$this->userRepo = $userRepo;
$this->requestStack = $requestStack;
}
public function onJWTCreated(JWTCreatedEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
$payload = $event->getData();
$user = $this->userRepo->findOneByUserName($payload['username']);
$payload['id'] = $user->getId();
$payload['email'] = $user->getEmail();
$payload['name'] = $user->getName();
$payload['lastName'] = $user->getLastName();
$payload['occupation'] = $user->getOccupation();
$payload['phoneExternal'] = $user->getPhoneExternal();
$payload['phoneExtension'] = $user->getphoneExtension();
$payload['googleScholarLink'] = $user->getGoogleScholarLink();
$payload['researchGateLink'] = $user->getresearchGateLink();
$payload['customRoles'] = $user->getCustomRoles();
$event->setData($payload);
$header = $event->getHeader();
$header['cty'] = 'JWT';
$event->setHeader($header);
}
}