<?php
namespace App\EventListener;
use App\Model\Error;
use Lexik\Bundle\JWTAuthenticationBundle\Event\{JWTExpiredEvent, JWTFailureEventInterface, JWTNotFoundEvent};
use Lexik\Bundle\JWTAuthenticationBundle\Exception\UserNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
class JwtEventListener
{
public function onJwtFailure(JWTFailureEventInterface $event)
{
if( $event->getException() instanceof UserNotFoundException ) {
return $event->setResponse(JsonResponse::create(new Error('Unauthorized'), JsonResponse::HTTP_UNAUTHORIZED));
}
if( $event instanceof JWTExpiredEvent ) {
return $event->setResponse(JsonResponse::create(new Error('Expired'), JsonResponse::HTTP_UNAUTHORIZED));
}
if( $event instanceof JWTNotFoundEvent ) {
return $event->setResponse(JsonResponse::create(new Error('Not allowed'), JsonResponse::HTTP_FORBIDDEN));
}
return $event->setResponse(JsonResponse::create(new Error('Unauthorized'), JsonResponse::HTTP_UNAUTHORIZED));
}
}