headers->has('X-AUTH-TOKEN'); } public function authenticate(Request $request): Passport { $apiToken = $request->headers->get('X-AUTH-TOKEN'); if (null === $apiToken) { return null; } $userIdentifier = $apiToken; return new SelfValidatingPassport( new UserBadge($userIdentifier, function (string $userIdentifier): ?UserInterface { return $this->userRepository->findOneBy([ 'authName' => $userIdentifier ]); })); } public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response { // on success, let the request continue return null; } public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response { $this->logger->critical("YYY"); $message = strtr($exception->getMessageKey(), $exception->getMessageData()); // or to translate this message // $this->translator->trans($exception->getMessageKey(), $exception->getMessageData()) // This should translated by FOSRestBundle! throw new AccessDeniedHttpException($message); } }