【问题标题】:Hash::check() return false in laravel 5Hash::check() 在 laravel 5 中返回 false
【发布时间】:2016-04-15 15:28:46
【问题描述】:

我刚开始使用 laravel 5,我正在做一个简单的登录功能来检查用户传递的电子邮件和密码是否与存储在数据库中的电子邮件和密码匹配。我一直在阅读文档 ([https://laravel.com/docs/5.0/hashing1) 但Hash::check($content['password'], $user->{'password'}) 总是返回 false。我的代码如下所示。

当我创建一个新用户时,我会像这样散列密码:

$content = json_decode($request->getContent(), true);

$user -> password = Hash::make($content['email']);

我的登录功能是这样的:

public function login(Request $request)
{
    $content = json_decode($request -> getContent(), true);

    $user = DB::table('users')->where('email', $content['email'])->first();


    if (Hash::check($content['password'], $user->{'password'}))
    {
       // Redirect to dashboard
    }
}

提前致谢!!

【问题讨论】:

  • 你试过把if (Hash::check($content['password'], $user->{'password'}))改成if (Hash::check($content['password'], $user->password))吗?
  • 我得到了同样的结果...
  • @IcarKwon 我希望我的回答能解决这个问题:)

标签: laravel authentication laravel-5


【解决方案1】:

实际上,您在创建用户时正在对电子邮件而不是密码进行哈希处理。修改代码

$user->password = Hash::make($content['email']);

$user->password = Hash::make($content['password']);

【讨论】:

  • 天哪……真可惜。我花了最后 3 个小时...哈哈哈谢谢@Jinu P C
【解决方案2】:

我想出了同样的问题。检查数据库用户表,密码字段。使字段的大小为 60 或更大。这个固定我的。

【讨论】:

  • 我的密码字段是 VARCHAR 255 长度 :(。无论如何谢谢!!:)
【解决方案3】:

外观哈希只会加密您的数据:

Hash::make('123456');

同理:

$password = bcrypt('123456');

要登录用户,您需要使用 AuthController 函数:

Auth::attempt(['email' => 'test@test.com' , 'password' => Hash::make('password')]);

这是一个例子。

如果您收到请求,可以将此方法添加到登录:

if(Auth::attempt(['email'   =>  $request->email, 'password'    =>  $request->password , 'active'    =>  1])){
        flash()->success('Successfully logged in!');
        return redirect('/');
}

尝试函数将对您的密码字段进行哈希处理,并将与数据库数据进行比较。

【讨论】:

    猜你喜欢
    • 2014-02-25
    • 1970-01-01
    • 2021-06-05
    • 2019-08-15
    • 2020-02-14
    • 2018-09-08
    • 2018-01-07
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多