Laravel 4 Auth::attempt 使用电子邮件或用户名和密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21694983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:00:51  来源:igfitidea点击:

Laravel 4 Auth::attempt using either email or username and password

phplaravellaravel-4

提问by Chinmoy

In laravel for login attempt I generally use something like this:

在 Laravel 中进行登录尝试,我通常使用这样的方法:

if (Auth::attempt(array('email' => $usernameinput, 'password' => $password), true))
{
    // The user is being remembered...
}

Basically $usernameinputwill check with emailfrom my table.

基本上$usernameinputemail从我的桌子上检查。

I was thinking to do it in a different way, like there is email, usernameand passwordin my table. $usernameinputcan be either emailor usernamefield in my table.

我想以不同的方式来做,就像有email,usernamepassword在我的桌子上。$usernameinput可以是我的表中的emailusername字段。

How can I Auth::attemptwith a condition like:

我怎么能Auth::attempt有这样的条件:

(email==$usernameinput OR username==$usernameinput) AND password == $password

回答by rmobis

Well, you could simply check if $usernameinputmatches an email pattern and if it does, use the emailfield, else use the usernamefield. This sounds reasonable because, well, you really shouldn't have any e-mail in your database that doesn't match the pattern. Something like this:

好吧,您可以简单地检查是否$usernameinput与电子邮件模式匹配,如果匹配,则使用该email字段,否则使用该username字段。这听起来很合理,因为您的数据库中确实不应该有任何与模式不匹配的电子邮件。像这样的东西:

$field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

if (Auth::attempt([$field => $usernameinput, 'password' => $password], true)) {
    // ...
}

The other option is to extend Illuminate\Auth\Guardadding the wanted functionality and setting it as the authcomponent, in a new service provider.

另一种选择是在新的服务提供者中扩展Illuminate\Auth\Guard添加所需的功能并将其设置为auth组件。

回答by ssi-anik

It can be done something like this

它可以做这样的事情

$field = Validator::make(array('email' => $usernameinput, array('email' => 'email'))->passes() ? 'email' : 'username';
if (Auth::attempt(array($field => $usernameinput, 'password' => $password), true)) {
    return Redirect::intended('profile');
}

回答by patsplat

Users could use an email field as their username, and it might not match the email they used for the email field. Safer to attempt authentication twice:

用户可以使用电子邮件字段作为他们的用户名,但它可能与他们用于电子邮件字段的电子邮件不匹配。尝试两次身份验证更安全:

if (Auth::attempt(array(
            'email'     => Input::get('email_or_username'),
            'password'  => Input::get('password')
        )) ||
    Auth::attempt(array(
            'username'     => Input::get('email_or_username'),
            'password'  => Input::get('password')
        ))) {
    // SUCCESS
} else {
    // FAILURE
}

回答by Parth Vora

Here's the complete working solution: (Tested with Laravel 5.3)

这是完整的工作解决方案:(用 Laravel 5.3 测试)

Just add this method(or we can say override) into app\Http\Controllers\Auth\LoginController.php

只需将此方法(或者我们可以说是覆盖)添加到app\Http\Controllers\Auth\LoginController.php

public function login(Request $request)
    {
        $usernameinput = $request->input('email');
        $password = $request->input('password');
        $field = filter_var($usernameinput, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

        if (Auth::attempt([$field => $usernameinput, 'password' => $password])) {
            return redirect()->intended('/');
        } else {
            return $this->sendFailedLoginResponse($request);
        }
    }