<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Token\JWTUserToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\AuthorizationHeaderTokenExtractor;
class JwtTokenListener
{
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* Add JWT token to the token storage
* If a JWT token is found in the request header, it is added to the token storage
* use it later if needed (for example in a custom voter)
*
* @param RequestEvent $event
* @return void
*/
public function onKernelRequest(RequestEvent $event)
{
$extractor = new AuthorizationHeaderTokenExtractor(
'Bearer',
'Authorization'
);
$token = $extractor->extract($event->getRequest());
if ($token) {
$jwtUserToken = new JWTUserToken();
$jwtUserToken->setRawToken($token);
$this->tokenStorage->setToken($jwtUserToken);
}
}
}