php Laravel 5 Auth:这些凭据与我们的记录不匹配

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

Laravel 5 Auth: These credentials do not match our records

phpauthenticationlaravellaravel-5

提问by Halnex

I'm just starting out with Laravel 5, I come from Laravel 4 environment so it shouldn't be too hard.

我刚开始使用 Laravel 5,我来自 Laravel 4 环境,所以应该不会太难。

I heard L5 comes with a built-in authentication system which is neat. I've set everything up from database to views.

我听说 L5 带有一个很整洁的内置身份验证系统。我已经设置了从数据库到视图的所有内容。

The registration process is working correctly and after that it logs me in automatically. but when I log out and try to log back in, I get this error:

注册过程正常工作,之后它会自动让我登录。但是当我注销并尝试重新登录时,我收到此错误:

These credentials do not match our records.

这些凭据与我们的记录不符。

I'm not sure what's wrong. do I have to write the login controller manually or how does it work in L5?

我不确定出了什么问题。我必须手动编写登录控制器还是在 L5 中它是如何工作的?

回答by mervasdayi

I had the same issue. The reason for mine was that I defined setPasswordAttributein my Usermodel so every time, I enter plain password, it hashes before sending to DB.

我遇到过同样的问题。我的原因是我setPasswordAttribute在我的User模型中定义了所以每次我输入普通密码时,它都会在发送到数据库之前散列。

public function setPasswordAttribute($password)
{
    $this->attributes['password'] = \Hash::make($password);
}

and in my db:seed, I was creating a user with hashed password with Hash::make("password"), too. So laravel hashes hashed password :)

在我的 db:seed 中,我也创建了一个带有散列密码的用户Hash::make("password")。所以 laravel 散列散列密码:)

In laravel version 5.* you don't need to hash input password for Auth, because Auth manages it itself. you just have to pass {{csrf_field()}}through form.

在 Laravel 版本 5.* 中,您不需要为 Auth 散列输入密码,因为 Auth 自行管理它。你只需要{{csrf_field()}}通过表格。

回答by Gerard Reches

In addition to @mervasdayisolution, a good way to hash passwords in setPasswordAttributeavoiding rehashing problems could be this:

除了@mervasdayi解决方案,散列密码以setPasswordAttribute避免重新散列问题的一个好方法可能是:

public function setPasswordAttribute($password){
    $this->attributes['password'] = Hash::needsRehash($password) ? Hash::make($password) : $password;
}

回答by Grant

Further to what @mervasdayi & Gerard Reches have suggested. Just thought I'd make a note that you will need to include

除了@mervasdayi 和 Gerard Reches 的建议之外。只是想我会做一个笔记,你需要包括

use Illuminate\Support\Facades\Hash;

at the top of your Usermodel when adding in these fixes.

User添加这些修复程序时,在模型的顶部。

回答by SANDWIDI

I think that it is later but i found two solutions to solve this problem. Firstly you can use bcrypt function if you use laravel 5.3. Look at the below function. It means that your get your data in array.

我认为是晚了,但我找到了两个解决方案来解决这个问题。首先,如果您使用 laravel 5.3,则可以使用 bcrypt 函数。看看下面的函数。这意味着您将数据放入数组中。

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

Secondly you can use mutator to fix it like this:

其次,您可以使用 mutator 来修复它,如下所示:

 public function setPasswordAttribute($password)
{
    $this->attributes['password'] = \Hash::make($password);
}

Hope that it can help others. Best regards

希望它可以帮助其他人。此致