Skip to content

Commit

Permalink
Merge pull request #28 from cristianopacheco/master
Browse files Browse the repository at this point in the history
Added comments in LoginController.
  • Loading branch information
vedovelli authored Dec 2, 2016
2 parents 2aef1f7 + 23cc4a7 commit df23ef0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions webservice/app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,76 @@ 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();

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');

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(
Expand Down

0 comments on commit df23ef0

Please sign in to comment.