/** * Authenticate user with credentials. * * @param Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function login(Request $request) { $credentials = $request->only('email', 'password'); if (!($token = $this->auth->attempt($credentials))) { return response()->json(['error' => 'invalid_credentials'], 401); } $user = $this->auth->setToken($token)->toUser(); return response()->json(['data' => compact('token', 'user')]); }
public function login(Request $request) { $credentials = $request->only('email', 'password'); try { // verify the credentials and create a token for the user if (!($token = JWTAuth::attempt($credentials))) { return $this->response->errorUnauthorized(); } } catch (JWTException $e) { // something went wrong return $this->response->errorInternal(); } return $this->response->array(['token' => $token]); }
public function backend(Request $request) { // grab credentials from the request $credentials = $request->only('email', 'password'); try { // attempt to verify the credentials and create a token for the user if (!($token = $this->auth->attempt($credentials))) { return response()->json(['error' => 'invalid_credentials'], 401); } } catch (JWTException $e) { // something went wrong whilst attempting to encode the token return response()->json(['error' => 'could_not_create_token'], 500); } // all good so return the token return response()->json(compact('token')); }
public function authenticate(Request $request, UserRepository $userRepository) { // grab credentials from the request $credentials = $request->only('email', 'password'); // TODO Move user authentication code somewhere more appropriate $previousException = null; if ($credentials["email"] === null) { $previousException = new NotFoundHttpException("User not found.", null, 0xc00101); } if ($credentials["password"] === null) { // TODO Test after code \Eos\Exceptions\Factory::collection() throw new NotFoundHttpException("User not found.", $previousException, 0xc00102); } else { if ($previousException !== null) { throw $previousException; } } // Try to find user by email $user = $userRepository->findWhere(["email" => $credentials["email"]]); if (count($user) === 0) { // The user could not found by that email throw new NotFoundHttpException("User not found.", null, 0xc00103); } /** * @var \Eos\Entities\User $user */ $user = $user[0]; if (!Hash::check($credentials["password"], $user->password)) { // Password mismatch throw new NotFoundHttpException("User not found.", null, 0xc00104); } try { $token = JWTAuth::fromUser($user); } catch (JWTException $exception) { throw new \Exception("Couldn't create token", 0xc00105); } return response()->json(["token" => $token, "user" => ["first_name" => $user->first_name, "middle_name" => $user->middle_name, "last_name" => $user->last_name, "email" => $user->email]]); }
function declineUser(Request $request) { $data = $request->only('token'); \DB::table('tmp_users')->where('token', '=', $data['token'])->delete(); return $this->response->noContent(); }