Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save AlexanderPoellmann/61feafa59963854009b7 to your computer and use it in GitHub Desktop.
Save AlexanderPoellmann/61feafa59963854009b7 to your computer and use it in GitHub Desktop.
Log in with Username or Email in Laravel 5 - AuthController->postLogin()
<?php
/**
* This snippet goes into your
* \app\Http\Controllers\Auth\AuthController.php
*
* Make sure to update your login view as well
* (\resources\views\auth\login.blade.php). Just
* change
*/
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
// get our login input
$login = $request->input('login');
// check login field
$login_type = filter_var( $login, FILTER_VALIDATE_EMAIL ) ? 'email' : 'username';
// merge our login field into the request with either email or username as key
$request->merge([ $login_type => $login ]);
// let's validate and set our credentials
if ( $login_type == 'email' ) {
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
$credentials = $request->only( 'email', 'password' );
} else {
$this->validate($request, [
'username' => 'required',
'password' => 'required',
]);
$credentials = $request->only( 'username', 'password' );
}
if ($this->auth->attempt($credentials, $request->has('remember')))
{
return redirect()->intended($this->redirectPath());
}
return redirect($this->loginPath())
->withInput($request->only('login', 'remember'))
->withErrors([
'login' => $this->getFailedLoginMessage(),
]);
}
@Tolerant
Copy link

@ptaking, just change email field name to login.

@anshul32
Copy link

is there any way to login with both username and email without any change in AuthenticatesUsers trait?

@alaa442
Copy link

alaa442 commented Jan 31, 2016

I have an exception when using this code which is:
Call to undefined method Illuminate\Support\Facades\Request::input()
help please >>>

@morloderex
Copy link

@alaa442 Remember to inport the Illuminate\Http\Request class! :)

@bflydesign
Copy link

bflydesign commented Jun 6, 2016

in this snippet the $auth is not a valid property:

Undefined property: App\Http\Controllers\Auth\AuthController::$auth (on line 49 in your snippet)

@oli-laban
Copy link

@bflydesign
Make sure you have use Auth; (it should be there already) and replace:
if ($this->auth->attempt($credentials, $request->has('remember')))
with
if (Auth::attempt($credentials, $request->has('remember')))

@elton-fonseca
Copy link

Thank you very much!

Only needed add use Auth and alter for if (Auth::attempt($credentials, $request->has('remember')))

@silviucs
Copy link

@ptaking - a little outdated, but I think he wanted to say to change the field type from email to text ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment