问题描述
我正在使用身份验证模块,我遇到这个奇怪的问题,Auth::attempt
返回false,我的用户名和密码正确.
I am working on authentication module, I have this weird problem, Auth::attempt
returns false, Where my username and password is correct.
在这里也问了相同的问题,但这没有解决我的问题问题,而这个问题是关于laravel 4的,我已经尝试了他们的方法,但是仍然无法使用.
The same question is asked here also but that is not addressing my problems and that question is about laravel 4, I have tried their methods but still not working.
我的控制器中包含以下代码:
I have the following code in my controller:
$user = array(
'name' => Input::get('username'),
'password' => Input::get('password')
);
if (Auth::attempt($user)) {
return "ok.";
} else {
dd($user);
}
其他部分返回:
array:2 [▼
"name" => "ali"
"password" => "ali"
]
这表示name
并且密码正确.
我使用的是laravel 5.2,在我的users
表中,密码未进行散列,并且没有remember_token
表示我直接创建了user
.
I am using laravel 5.2 and in my users
table the password is not hashed and there is no remember_token
means i have created the user
directly.
推荐答案
这将不起作用,因为auth :: attempt使用bcrypt将密码转换为哈希,并在用户表中查找该哈希以进行匹配.
this will not work because auth::attempt converts password to hash using bcrypt, and looks for that hash in users table to match.
简而言之,密码应该是存储在数据库表中的哈希值,以便auth ::尝试工作.
in short the password should be a hash stored in database table for auth::attempt to work.
这就是为什么if()条件失败的原因.
that is why your if() condition failing.
以下来自laravel 5.2文档
below is from laravel 5.2 docs
如果身份验证成功,则try方法将返回true. 否则,将返回false.
The attempt method will return true if authentication was successful. Otherwise, false will be returned.
这篇关于Laravel 5.2:身份验证auth :: attempt返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!