简体   繁体   English

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

[英]Laravel 5.2 : Authentication auth::attempt returns false

I am working on authentication module, I have this weird problem, Auth::attempt returns false, Where my username and password is correct. 我正在研究认证模块,我有这个奇怪的问题, Auth::attempt返回false,我的用户名和密码是正确的。

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. 这里也问了同样的问题,但这并没有解决我的问题,这个问题是关于laravel 4,我已经尝试了他们的方法,但仍然没有工作。

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

The else part returns : else部分返回:

array:2 [▼
  "name" => "ali"
  "password" => "ali"
]

Which means the name and the password is correct. 这意味着name和密码是正确的。

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. 我正在使用laravel 5.2,在我的users表中,密码没有哈希,没有remember_token意味着我已经直接创建了user

try方法在将密码与数据库进行比较之前对其进行哈希处理,这意味着如果数据库中的密码未经过哈希处理,则不会匹配。

this will not work because auth::attempt converts password to hash using bcrypt, and looks for that hash in users table to match. 这不起作用,因为auth :: attempt使用bcrypt将密码转换为hash,并在users表中查找匹配的hash。

in short the password should be a hash stored in database table for auth::attempt to work. 简而言之,密码应该是存储在数据库表中的哈希值,用于auth ::尝试工作。

that is why your if() condition failing. 这就是你的if()条件失败的原因。

below is from laravel 5.2 docs 以下是来自laravel 5.2 docs

The attempt method accepts an array of key / value pairs as its first argument. attempt方法接受一个键/值对数组作为其第一个参数。 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. 如果两个散列密码匹配,则将为用户启动经过身份验证的会话。

The attempt method will return true if authentication was successful. 如果身份验证成功,则尝试方法将返回true。 Otherwise, false will be returned. 否则,将返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM