Exemple #1
0
 /**
  * @Route("/auth/token")
  */
 public function tokenAction(Request $request)
 {
     $result = new Result();
     $username = $request->request->get('username');
     $password = $request->request->get('password');
     $user = $this->get("api.user_repository")->findOneBy(["username" => $username]);
     if ($user == null) {
         return new JsonResponse($result->addError("Username was not found.")->toArray());
     }
     if ($user->getPassword() == $this->get('security.password_encoder')->encodePassword($user, $password)) {
         $token = $this->get('token_helper')->encode(['username' => $user->getUsername()], 600);
         return new JsonResponse($result->setData(["token" => $token])->toArray());
     } else {
         return new JsonResponse($result->addError("Incorrect password.")->toArray());
     }
 }
Exemple #2
0
 public function start(Request $request, AuthenticationException $exception = null)
 {
     $result = new Result();
     return new JsonResponse($result->addError($exception != null ? $exception->getMessage() : "Authentication required.")->toArray(), 401);
 }