laravel 修改现有的授权模块(电子邮件到用户名)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28584531/
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 10:58:24  来源:igfitidea点击:

Modify existing Authorization module (email to username)

laravellaravel-5

提问by Renesansz

I would like to modify the existing Authorization module provided by Laravel 5, instead of asking for the emailit will ask for the usernamefield in the database.

我想修改 Laravel 5 提供的现有授权模块,而不是要求email它会要求username数据库中的字段。

回答by Alexandre Chopin

Laravel search the variable $username in the file :

Laravel 在文件中搜索变量 $username :

Illuminate\Foundation\Auth\AuthenticatesUsers

Illuminate\Foundation\Auth\AuthenticatesUsers

public function loginUsername() {
    return property_exists($this, 'username') ? $this->username : 'email';
}

As you can see, by default it will be named as 'email'.

如您所见,默认情况下它将被命名为“电子邮件”。

However you can override it in your AuthController by adding :

但是,您可以通过添加以下内容在您的 AuthController 中覆盖它:

protected $username = 'username';

回答by Joe

You do not need to modify the Auth module to do this, simply pass the user's identifier in the attempt. Use the field name in the attempt array as such:

您不需要修改 Auth 模块来执行此操作,只需在尝试中传递用户的标识符即可。在尝试数组中使用字段名称,如下所示:

if (Auth::attempt(['username' => $username, 'password' => $password]))
    {
        return redirect()->intended('dashboard');
    }

回答by majidarif

You can try to check the file Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsersjust to get the idea.

您可以尝试检查该文件Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers以获得想法。

Then add an override of postLoginon your AuthController:

然后postLogin在你的上添加一个覆盖AuthController

public function postLogin(Request $request)
{
    $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('username', 'remember'))
                ->withErrors([
                    'username' => 'These credentials do not match our records.',
                ]);
}

You also need to add use Illuminate\Http\Request;to your AuthController.

您还需要添加use Illuminate\Http\Request;到您的AuthController.

回答by Irakli Dgebuadze

you can just override auth username function from LoginController.php in laravel 5.3

您可以在 laravel 5.3 中从 LoginController.php 覆盖 auth 用户名功能

public function username(){ 
   return 'username';
}

回答by hamid Reza Kamali

in controllers\auth\logincontroller add this

在controllers\auth\logincontroller 添加这个

 protected $username = 'user_name';//user_name field name

then go to Illuminate\Foundation\Auth\AuthenticatesUsers and change

然后转到 Illuminate\Foundation\Auth\AuthenticatesUsers 并更改

 public function username()
    {
         return 'email';//change this with "return $this->username;"
    }

with this method You can have different log in type in different controller for example in another controller controllers\admin_auth\logincontroller

使用此方法您可以在不同的控制器中使用不同的登录类型,例如在另一个控制器控制器\admin_auth\logincontroller

protected $username = 'phone_number';