现在我已经按照Laravel的文档介绍了如何在认证过程中允许用户名,但是这会消除使用电子邮件的能力。我想让用户使用他们的用户名或电子邮件登录。我如何去做这件事?允许在Laravel 5.4中使用用户名或电子邮件登录
我已经按照Laravel的文档将此代码添加到LoginController,并且它只允许用户名登录。我希望它接受用户名或电子邮件登录。从这个链接
public function username() {
return 'username';
}
现在我已经按照Laravel的文档介绍了如何在认证过程中允许用户名,但是这会消除使用电子邮件的能力。我想让用户使用他们的用户名或电子邮件登录。我如何去做这件事?允许在Laravel 5.4中使用用户名或电子邮件登录
我已经按照Laravel的文档将此代码添加到LoginController,并且它只允许用户名登录。我希望它接受用户名或电子邮件登录。从这个链接
public function username() {
return 'username';
}
遵照说明:https://laravel.com/docs/5.4/authentication#authenticating-users
然后你就可以检查用户输入这样
$username = $request->username; //the input field has name='username' in form
if(filter_var($username, FILTER_VALIDATE_EMAIL)) {
//user sent their email
Auth::attempt(['email' => $username, 'password' => $password]);
} else {
//they sent their username instead
Auth::attempt(['username' => $username, 'password' => $password]);
}
//was any of those correct ?
if (Auth::check()) {
//send them where they are going
return redirect()->intended('dashboard');
}
//Nope, something wrong during authentication
return redirect()->back()->withErrors([
'credentials' => 'Please, check your credentials'
]);
这仅仅是一个样品。你可以采取无数种方法来实现同样的目标。
我认为一个简单的方法是只覆盖的用户名法的LoginController:
public function username()
{
$login = request()->input('login');
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$field => $login]);
return $field;
}
我认为它更加简单,只需编辑AuthenticatesUsers特质,凭证的方法。在这里我已经实现了使用电子邮件或电话登录。你可以改变它以适应你的需求。
protected function credentials(Request $request)
{
if(is_numeric($request->get('email'))){
return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
}
return $request->only($this->username(), 'password');
}
This solution of "Rabah G" works for me in Laravel 5.2. I modified a litle but is the same
$loginType = request()->input('useroremail');
$this->username = filter_var($loginType, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
request()->merge([$this->username => $loginType]);
return property_exists($this, 'username') ? $this->username : 'email';
谢谢,这是我感谢你的解决方案。
保护功能的凭据(请求$要求){
$login = request()->input('email');
// Hayamos el tipo campo del que se trata, email o username.
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'user_name';
return [
$field => $request->get('email'),
'password' => $request->password,
'verified' => 1
];
}
你需要从\Illuminate\Foundation\Auth\AuthenticatesUsers
特质覆盖protected function attemptLogin(Request $request)
方法在你的LoginController 即在我的LoginController类
protected function attemptLogin(Request $request) {
$identity = $request->get("usernameOrEmail");
$password = $request->get("password");
return \Auth::attempt([
filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'username' => $identity,
'password' => $password
]);
}
您的LoginController类应使用Trait \Illuminate\Foundation\Auth\AuthenticatesUsers
以覆盖attemptLogin
方法即
class LoginController extends Controller {
use \Illuminate\Foundation\Auth\AuthenticatesUsers;
.......
.......
}
您是否正在使用laravel的默认身份验证?如果是的话,你必须选择用户名或电子邮件。不是都。 usrname返回一个字符串,而不是一个数组。如果你想自定义登录,你必须自己编写逻辑。 – EddyTheDove
我该怎么做?如果我想写自己的逻辑,我不确定要编辑哪些文件。 –
查看下面的答案。 – EddyTheDove