Created
December 6, 2017 06:02
-
-
Save axieum/60f4ae05c4b85eccf7d685dac584124f to your computer and use it in GitHub Desktop.
Laravel Authentication via Username or Email
This file contains 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 | |
namespace App\Http\Controllers\Auth; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Foundation\Auth\AuthenticatesUsers; | |
use Illuminate\Http\Request; | |
class LoginController extends Controller | |
{ | |
/* | |
|-------------------------------------------------------------------------- | |
| Login Controller | |
|-------------------------------------------------------------------------- | |
| | |
| This controller handles authenticating users for the application and | |
| redirecting them to your home screen. The controller uses a trait | |
| to conveniently provide its functionality to your applications. | |
| | |
*/ | |
use AuthenticatesUsers; | |
/** | |
* Where to redirect users after login. | |
* | |
* @var string | |
*/ | |
protected $redirectTo = '/home'; | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('guest')->except('logout'); | |
} | |
/** | |
* Get the needed authorization credentials from the request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return array | |
*/ | |
protected function credentials(Request $request) | |
{ | |
$field = filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ? 'email' : 'username'; | |
$request->merge([$field => $request->input($this->username())]); | |
return $request->only($field, 'password'); | |
} | |
/** | |
* Get the login username to be used by the controller. | |
* | |
* @return string | |
*/ | |
public function username() | |
{ | |
// This is the name of the field that contains the email/username. | |
return 'login'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment