Forked from AlexanderPoellmann/laravel-5-tweak-login-authcontroller.php
Created
February 16, 2016 14:30
-
-
Save mickby/241e6b73b42a746635b6 to your computer and use it in GitHub Desktop.
Log in with Username or Email in Laravel 5 - AuthController->postLogin()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(), | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment