<?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)
{
$exception = $event->getException();
$data['error'] = true;
if ($exception->getMessageKey() === 'unmatched_account') {
$data = array(
'message' => 'unmatched_account'
);
} elseif ($exception->getMessageKey() === 'email_not_verified') {
$data = array(
'message' => 'email_not_verified'
);
} elseif ($exception->getMessageKey() === 'account_not_validated') {
$data = array(
'message' => 'account_not_validated'
);
} else {
$data = array(
'error' => true,
'message' => 'invalid_credentials'
);
}
$data['error'] = true;
$response = new JWTAuthenticationFailureResponse('', JsonResponse::HTTP_FORBIDDEN);
$response->setData($data);
$event->setResponse($response);
}
}