diff --git a/webservice/app/Http/Controllers/LoginController.php b/webservice/app/Http/Controllers/LoginController.php index 92b79c3..0bfc4ae 100644 --- a/webservice/app/Http/Controllers/LoginController.php +++ b/webservice/app/Http/Controllers/LoginController.php @@ -12,27 +12,50 @@ class LoginController extends Controller { use ThrottlesLogins; + /** + * Issue a JWT token when valid login credentials are + * presented. + * + * @param Request $request + * @return \Illuminate\Http\JsonResponse + */ public function login(Request $request) { + // Determine if the user has too many failed login attempts. if ($this->hasTooManyLoginAttempts($request)) { + + // Fire an event when a lockout occurs. $this->fireLockoutEvent($request); return $this->sendLockoutResponse($request); } + // Grab credentials from the request. $credentials = $request->only('email', 'password'); + // Attempt to verify the credentials and create a token for the user. if ($token = Auth::guard('api')->attempt($credentials)) { + + // All good so return the json with token and user. return $this->sendLoginResponse($request, $token); } + // Increments login attempts. $this->incrementLoginAttempts($request); return $this->sendFailedLoginResponse($request); } + /** + * Return the token and current user authenticated. + * + * @param Request $request + * @param $token + * @return \Illuminate\Http\JsonResponse + */ protected function sendLoginResponse(Request $request, $token) { + // Clear the login locks for the given user credentials. $this->clearLoginAttempts($request); $user = Auth::guard('api')->user(); @@ -40,6 +63,12 @@ protected function sendLoginResponse(Request $request, $token) return response()->json(compact('token', 'user')); } + /** + * Return error message after determining invalid credentials. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\JsonResponse + */ protected function sendFailedLoginResponse(Request $request) { $message = Lang::get('auth.failed'); @@ -47,6 +76,12 @@ protected function sendFailedLoginResponse(Request $request) return response()->json(['messages' => [$message]], 401); } + /** + * Redirect the user after determining they are locked out. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\JsonResponse + */ public function sendLockoutResponse(Request $request) { $seconds = $this->limiter()->availableIn(