<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
class JWTInvalidListener
{
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function onJWTInvalid(JWTInvalidEvent $event): void
{
$response = new JWTAuthenticationFailureResponse($this->translator->trans(
'Your token is invalid, please login again to get a new one.', [], 'security'
), Response::HTTP_UNAUTHORIZED);
$event->setResponse($response);
}
}