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

Ren*_*nsz 12 laravel laravel-5

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

Ale*_*pin 35

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

照亮\基金会\验证\ AuthenticatesUsers

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

如您所见,默认情况下,它将被命名为"email".

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

protected $username = 'username';
Run Code Online (Sandbox Code Playgroud)


Joe*_*Joe 5

您无需修改​​Auth模块来执行此操作,只需在尝试中传递用户的标识符即可.在try尝试数组中使用字段名称:

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


maj*_*rif 5

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

然后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.',
                ]);
}
Run Code Online (Sandbox Code Playgroud)

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


小智 5

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

public function username(){ 
   return 'username';
}
Run Code Online (Sandbox Code Playgroud)