<?php
namespace App\Listeners;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationSuccessResponse;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class AuthenticationSuccessListener
{
private $jwtTokenTTL;
private $secureCookie = false;
public function __construct($ttl)
{
$this->jwtTokenTTL = $ttl;
}
/**
* This function is responsible for the authentication part
*
* @param AuthenticationSuccessEvent $event
* @return JWTAuthenticationSuccessResponse
*/
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
/** @var JWTAuthenticationSuccessResponse $response */
$response = $event->getResponse();
$data = $event->getData();
$tokenJWT = $data['token'];
unset($data['token']);
unset($data['refresh_token']);
$user = $event->getUser();
$data['user'] = [
'id' => $user->getId(),
'username' => $user->getUsername(),
'displayname' => $user->getDisplayName(),
'firstname' => $user->getFirstName(),
'prefix' => $user->getPrefix(),
'lastname' => $user->getLastName(),
'email' => $user->getEmail(),
'roles' => $user->getRoles(),
'isAdmin' => $user->getIsAdmin(),
'isEditor' => $user->getIsEditor(),
'isViewer' => $user->getIsViewer()
];
$event->setData($data);
$response->headers->setCookie(new Cookie('JWT', $tokenJWT, (
new \DateTime())
->add(new \DateInterval('PT' . $this->jwtTokenTTL . 'S')), '/', null, $this->secureCookie));
return $response;
}
}