Skip to content

Instantly share code, notes, and snippets.

@jokamjohn
Created September 22, 2015 05:50
Show Gist options
  • Save jokamjohn/76e9f77eac1189710e3a to your computer and use it in GitHub Desktop.
Save jokamjohn/76e9f77eac1189710e3a to your computer and use it in GitHub Desktop.
public function postLogin(Request $request)
{
//dd( $this->getCredentials($request) );
$authenticated = false;
$this->validate($request, [
$this->loginUsername() => 'required',
'password' => 'required'
]);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if (Auth::attempt($credentials, $request->has('remember'))) {
$authenticated = true;
}
$this->saveAttemptRecord($request, $authenticated);
if($authenticated){
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return redirect($this->loginPath())
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([
$this->loginUsername() => $this->getFailedLoginMessage(),
]);
}
/**
* Store the login attempt in the database
*
* @param Request $request
* @param Boolean $authenticated
* @return void
*/
private function saveAttemptRecord(Request $request, $authenticated)
{
$log = new ThrottleLog;
$log->username = $this->getUserName($request);
$log->attempts = $this->getLoginAttempts($request);
$log->ip_address = ip2long( $request->ip() );
$log->user_agent = $request->header('User-Agent') !== null ? $request->header('User-Agent') : '';
$log->result = $authenticated ? 'Pass' : 'Fail';
$log->save();
}
/**
* Get the userName that is attempting to login
*
* @param Request $request
* @return string username
*/
private function getUserName(Request $request)
{
return isset($request->only($this->loginUsername())[$this->loginUsername()]) ? $request->only($this->loginUsername())[$this->loginUsername()] : '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment