Laravel 5.2:身份验证auth :: attempt返回false

问题描述:

我正在使用身份验证模块,我遇到这个奇怪的问题,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方法接受键/值对数组作为其第一个 争论.数组中的值将用于在以下位置查找用户 您的数据库表.因此,在上面的示例中,用户将 通过电子邮件列的值检索.如果找到用户,则 存储在数据库中的哈希密码将与 通过数组传递给方法的哈希密码值.如果两个 匹配身份验证会话的哈希密码将针对 用户.

The attempt method accepts an array of key / value pairs as its first argument. The values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column. If the user is found, the hashed password stored in the database will be compared with the hashed password value passed to the method via the array. If the two hashed passwords match an authenticated session will be started for the user.

如果身份验证成功,则try方法将返回true. 否则,将返回false.

The attempt method will return true if authentication was successful. Otherwise, false will be returned.