php Laravel:如何更改默认的身份验证密码字段名称

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

Laravel: How can i change the default Auth Password field name

phplaravellaravel-5

提问by D.Intziadis

I'm currently working on my first laravel project and i'm facing a problem.

我目前正在开展我的第一个 Laravel 项目,但遇到了一个问题。

If you have experience with laravel you probably know that by calling php artisan make:authyou will get a predefined mechanism that handles login and registration.

如果您有使用 laravel 的经验,您可能知道通过调用php artisan make:auth您将获得处理登录和注册的预定义机制。

This mechanism is set to understand a couple of commonly used words in order to automate the whole procedure.

该机制设置为理解几个常用词,以便使整个过程自动化。

The problem that occurs in my case is that i'm using oracle db and it won't let me have a table column with the name of passwordbecause its a system keyword and it throws errors when trying to insert a user.

在我的情况下出现的问题是我使用的是 oracle db,它不会让我有一个名称为 的表列,password因为它是一个系统关键字,并且在尝试插入用户时会引发错误。

So far, i've tried to change my passwordcolumn to passwdand it worked in my registration form as expected. The User row was successfully inserted and my page was redirected to /home. Register

到目前为止,我已经尝试将我的password专栏更改为,passwd并且它在我的注册表中按预期工作。用户行已成功插入,我的页面被重定向到 /home。 登记

Success

成功

But when i try to logout and then relogin, i get this error telling me that my credentials are not correct. enter image description here

但是当我尝试注销然后重新登录时,我收到此错误消息,告诉我我的凭据不正确。 在此处输入图片说明

As for my code, i've changed my RegisterController.phpso that it takes username instead of email

至于我的代码,我已经更改了我的代码,RegisterController.php以便使用用户名而不是电子邮件

protected function validator(array $data)
{
    return Validator::make($data, [
        'username' => 'required|max:50|unique:ECON_USERS',
        'passwd' => 'required|min:6|confirmed',
    ]);
}

protected function create(array $data)
{
    return User::create([
        'username'   => $data['username'],
        'passwd'     => bcrypt($data['passwd'])
    ]);
}

The User $fillable

用户 $fillable

protected $fillable = [
    'username', 'passwd'
];

I'm guessing that Auth is trying to authenticate with emailand not usernameor that Auth is searching for passwordand not passwd

我猜 Auth 正在尝试使用email而不是进行身份验证,username或者 Auth 正在搜索password而不是passwd

回答by Gaurav

For having usernameinstead of email, you can overwrite username()in your LoginController.php

对于具有username替代email,可以覆盖用户名()在你的LoginController.php

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'username';
}

And for passwdinstead of password, you can do define an accessorin your App\User.php

而对于passwd代替password,您可以在App\User.php 中定义一个访问器

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->passwd;
}

login.blade.php: Replace emailinput with usernamebut do notchange the name of the input for password.

login.blade.php:将email输入替换为username更改输入的名称password

回答by Fernando Aires

Use this. It's work for me.

用这个。这对我有用。

So far I have changed the User.php

到目前为止,我已经更改了 User.php

public function getAuthPassword(){  
    return $this->senha;
}

and

public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }

Also on LoginController.php

也在 LoginController.php 上

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

回答by Dimitri Mostrey

In the app/Http/Controllers/Auth/LoginControlleroverride the default class by adding:

app/Http/Controllers/Auth/LoginController覆盖默认类时添加:

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required', 'passwd' => 'required',
    ]);
}

Don't forget to add use Illuminate\Http\Request;

不要忘记添加 use Illuminate\Http\Request;

It could be you have to add this too to your LoginController.

可能您也必须将它添加到您的LoginController.

/**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        return $request->only($this->username(), 'passwd');
    }

That should do it.

那应该这样做。

回答by Niyaz

In Laravel 5.7 beside above answer you must change EloquentUserProvider class. search in the file for 'password' word in lines (107,117, and 140) you found 'password' word and change it with new name, and this is all solustion.

在上述答案旁边的 Laravel 5.7 中,您必须更改 EloquentUserProvider 类。在文件中的第 107,117 和 140 行中搜索“密码”字,您找到了“密码”字并将其更改为新名称,这就是全部解决方案。

  • In User Model add this method :
  • 在用户模型中添加此方法:
public function getAuthPassword(){
    return $this->new_password_name;
}
public function getAuthPassword(){
    return $this->new_password_name;
}
  • In LoginController add this :
  • 在 LoginController 添加这个:
protected function validateLogin(Request $request){
    $this->validate($request, [
        $this->username() => 'required',
        'new_password_name' => 'required',
    ]);
}
protected function credentials(Request $request)
{
    return $request->only($this->username(), 'new_password_name');
}
public function username()
{
    return 'new_username';//or new email name if you changed
}
protected function validateLogin(Request $request){
    $this->validate($request, [
        $this->username() => 'required',
        'new_password_name' => 'required',
    ]);
}
protected function credentials(Request $request)
{
    return $request->only($this->username(), 'new_password_name');
}
public function username()
{
    return 'new_username';//or new email name if you changed
}
  • In login.blade.php change id and name of html element.
  • In EloquentUserProvider class inside validateCredentialsand retrieveByCredentialsfunction change 'password' word with the new name.
  • 在 login.blade.php 中更改 html 元素的 id 和名称。
  • 在 EloquentUserProvider 类中validateCredentialsretrieveByCredentials函数用新名称更改“密码”字样。

Edit:I change EloquentUserProviderclass but if you think changing laravel class is a bad practice you can create custom provider and override the retrieveByCredentialsand validateCredentialsfunctions in the custom provider.

编辑:我更改了EloquentUserProvider类,但是如果您认为更改 laravel 类是一种不好的做法,则可以创建自定义提供程序并覆盖自定义提供程序中的retrieveByCredentialsvalidateCredentials函数。

回答by alvirbismonte

If you are using the latest version. I am returning it like the following code below for a custom password field. On my end, I am using Lumen 6.x though it would apply to current version of Laravel also.

如果您使用的是最新版本。对于自定义密码字段,我将像下面的代码一样返回它。最后,我使用的是 Lumen 6.x,尽管它也适用于当前版本的 Laravel。

/**
 * Get the custom password field for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->custom_password_field_here;
}

回答by Ghanshyam Nakiya

IN Custom Controller

IN 自定义控制器

public function login(Request $request){

    if (Auth::attempt(['email' => $request->email,'password' => $request->password], false)){

         return redirect()->intended(route('subportal.dashboard'));
    }

    return $this->sendFailedLoginResponse($request);
}

protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->username() => 'required', 'password' => 'required',
    ]);
}

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function username()
{
    return 'email';
}

In App/Users.php

在 App/Users.php 中

public $table = "customer";
protected $primaryKey = 'cust_id';
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'pass', 'email','record_date',
];

public function getAuthPassword() {
    return $this->pass;
}

回答by Juliver Galleto

In your AuthController.php (Located in app/http/Controllers/Auth)

在您的 AuthController.php(位于 app/http/Controllers/Auth 中)

public function postLogin(Request $request)
{
    $this->validate($request, [
                'username' => 'required',
                'password' => 'required',
    ]);

    $credentials = ($request->only('username', 'password'));
    if ($this->auth->attempt($credentials)) {
       //do something, credentials is correct!   
    }
    return "Ops! snap! seems like you provide an invalid login credentials";

}