- <?php
- namespace App\EventSubscriber;
- use App\Repository\Main\UserRepository;
- use Doctrine\ORM\EntityManagerInterface;
- use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
- use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
- use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
- use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
- use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Contracts\Translation\TranslatorInterface;
- class JwtSubscriber implements EventSubscriberInterface
- {
-     protected $entityManager;
-     protected $userRepository;
-     protected $translator;
-     public function __construct(EntityManagerInterface $entityManager, UserRepository $userRepository, TranslatorInterface $translator)
-     {
-         $this->entityManager = $entityManager;
-         $this->userRepository = $userRepository;
-         $this->translator = $translator;
-     }
-     public function onExpired(JWTExpiredEvent $event)
-     {
-         throw new HttpException(401,'Expired JWT Token');
-     }
-     public function onNotFound(JWTNotFoundEvent $event)
-     {
-         $message = $event->getException()->getPrevious()->getMessage();
-         throw new HttpException(403, $message);
-     }
-     public function onInvalid(JWTInvalidEvent $event)
-     {
-         $message = $event->getException()->getPrevious()->getMessage();
-         throw new HttpException(401, $message);
-     }
-     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
-     {
- //        $message = $event->getException()->getPrevious()->getMessage();
-         throw new HttpException(401, $this->translator->trans('USERNAME_OR_PASSWORD_ARE_INCORRECT'));
-     }
-     public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
-     {
-         //
-     }
-     public static function getSubscribedEvents()
-     {
-         return [
-             'lexik_jwt_authentication.on_jwt_not_found' => 'onNotFound',
-             'lexik_jwt_authentication.on_jwt_expired' => 'onExpired',
-             'lexik_jwt_authentication.on_jwt_invalid' => 'onInvalid',
-             'lexik_jwt_authentication.on_authentication_failure' => 'onAuthenticationFailure',
-             'lexik_jwt_authentication.on_authentication_success' => 'onAuthenticationSuccess',
-         ];
-     }
- }