/** * @param Request $request * @param array $routeData * @throws AuthenticationException */ public function handle(Request &$request, array $routeData) { if (!$this->isAuthenticationRequired($routeData)) { return; } $tokenHeader = $request->headers->get('Authorization'); preg_match("/Bearer:? (.*)/", $tokenHeader, $output_array); if (count($tokenHeader) != 2) { throw new AuthenticationException('No authentication token provided', 401); } $this->authenticationService->authenticateWithToken($tokenHeader[1]); }
/** * @RPC\Route("/api/auth/login") * @RPC\Method("POST") */ public function login() { $credentials = json_decode($this->request->getContent(), true); try { v::create()->key('email', v::notEmpty())->key('password', v::notEmpty())->assert($credentials); } catch (ValidationException $e) { $errors = $e->findMessages(['email', 'password']); throw new \pmill\Doctrine\Rest\Exception\ValidationException($errors); } $password = $credentials['password']; unset($credentials['password']); /** @var User $user */ $user = $this->authenticationService->authenticateWithCredentials(User::class, $credentials, $password); $token = $this->authenticationService->generateTokenFromObject($user); return ['token' => $token]; }