Laravel:如何更改默认的身份验证密码字段名称? [英] Laravel: How can I change the default Auth Password field name?

查看:30
本文介绍了Laravel:如何更改默认的身份验证密码字段名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently working on my first laravel project and I am facing a problem.

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

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

此机制旨在理解几个常用词,以便自动完成整个过程.

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

在我的例子中出现的问题是我使用的是 oracle db,它不会让我有一个名为 password 的表列,因为它是一个系统关键字,并且在尝试插入用户.

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

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

So far, I have tried to change my password column to passwd and it worked in my registration form as expected. The User row was successfully inserted and my page was redirected to /home.

但是当我尝试注销然后重新登录时,我收到此错误消息,告诉我我的凭据不正确:

But when I try to logout and then relogin, I get this error telling me that my credentials are not correct:

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

As for my code, I have changed my RegisterController.php so that it takes username instead of email

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'])
    ]);
}

用户 $fillable

The User $fillable

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

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

I am guessing that Auth is trying to authenticate with email and not username or that Auth is searching for password and not passwd.

推荐答案

对于 username 而不是 email,您可以覆盖 username() 在你的 LoginController.php>

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

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

对于 passwd 而不是 password,你可以定义一个

And for passwd instead of password, you can do define an accessor in your AppUser.php

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

login.blade.php :用 username 替换 email 输入,但不要更改名称密码的输入.

login.blade.php : Replace email input with username but do not change the name of the input for password.

这篇关于Laravel:如何更改默认的身份验证密码字段名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆