<?php
namespace App\EventListener;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
class AuthenticationFailureListener
{
/**
* @param AuthenticationFailureEvent $event
*/
public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event)
{
if ($event->getException()->getMessage() == "Bad credentials."){
// Custom status code & error message
$code = JsonResponse::HTTP_UNAUTHORIZED;
$message = "Informations d'identification incorrectes, veuillez vérifier que votre nom d'utilisateur / mot de passe est correctement saisi";
$response = new JWTAuthenticationFailureResponse($message, $code);
$event->setResponse($response);
}
}
}