Skip to content

Instantly share code, notes, and snippets.

@jrsalunga
Created September 15, 2015 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrsalunga/179e9deceadb26117c0e to your computer and use it in GitHub Desktop.
Save jrsalunga/179e9deceadb26117c0e to your computer and use it in GitHub Desktop.
Laravel 5.1 - Log in with Username or Email
<?php
/**
* This snippet goes into your
* \app\Http\Controllers\Auth\AuthController.php
*
*
*/
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request) {
// $request->input('email') is from the form
$login_type = filter_var($request->input('email'), FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
$request->merge([ $login_type => $request->input('email')]);
if ($login_type == 'email') {
$this->validate($request, [
'email' => 'required|email', // the validation can be separate
'password' => 'required',
]);
} else {
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
}
/*
$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 = $request->only($login_type, 'password' );
//$credentials = $this->getCredentials($request);
if (Auth::attempt($credentials, $request->has('remember'))) {
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 $this->loginUsername();
return redirect($this->loginPath())
->withInput($request->only($this->loginUsername(), 'remember'))
->withErrors([
$this->loginUsername() => $this->getFailedLoginMessage(),
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment