<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
class AuthenticationFailureListener
{
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event): void
{
$response = new JWTAuthenticationFailureResponse($this->translator->trans(
'Bad credentials, please verify that your username/password are correctly set.', [], 'security'
), Response::HTTP_UNAUTHORIZED);
$event->setResponse($response);
}
}