<?php
namespace App\EventListener;
use App\Service\UserManager;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse;
class AuthenticationFailureListener
{
private $user_manager;
public function __construct(UserManager $user_manager)
{
$this->user_manager = $user_manager;
}
public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event){
$userName = $event->getException()->getToken()->getUsername()?$event->getException()->getToken()->getUsername():false;
if($userName){
$code = $this->user_manager->isUserEnabled($userName);
}else{
$code = 401;
}
$response = new JWTAuthenticationFailureResponse( 'Bad credentials' , $code);
$event->setResponse($response);
}
}