【问题标题】:Hash::check is always false or nullHash::check 始终为 false 或 null
【发布时间】:2020-06-27 18:45:16
【问题描述】:

为什么登录时 Hash::check 总是 false 或 null?

这是我的注册控制器的一部分。我保存在一个名为“密码”的表中的密码,我使用以下方法对其进行哈希处理

    ....
               $user = new Hause_users();
               $user->username = request('username');
               $user->Email = request('email');
               $user->password = Hash::make(request('password'));
               $user->First_Last_Name = request('first_last_name');


               $user->save();

下面是我使用 Hash::check 方法的登录控制器,我在其中检查当前用户,其在名为 Hause_users 的数据库中的哈希密码,我通过登录表单获取字符串输入密码,并尝试将其与哈希匹配::查看。在这种情况下,pass 必须是 123123。

问题是它总是返回“Bye”。

我在这里做错了什么?

<?php

namespace App\Http\Controllers\userController;


use App\Model\Hause_users;
use App\User;
use Illuminate\Http\Request;

use Illuminate\Support\Facades\Hash;

use Illuminate\Routing\Controller;


use Illuminate\Support\Facades\Response;


class loginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return Response
     */
    public function authLogin(Request $request)
    {
       $data = Hause_users::where('username', '=', request('username'))->first();
        $password = ($data['password']);


             // dd($request->password);
        //dd($data['password']);

        $check = Hash::check($request->password, $data['password']);
           if($check){
               return 'welcome';
        }else{
            return 'Bye';
        }

        //echo($pass_in);

        }

}

感谢您的建议,我正在使用 laravel 框架的 Hash::make 方法,该方法实际上是幕后的 bcript。我只能理解为什么 Laravel 提供的 Hash::check 与用户输入与从我的数据库中获取的密码不匹配。

【问题讨论】:

  • 谢谢,但不是真的。我正在使用 laravel 框架的 Hash::make 方法,它实际上是幕后的脚本。我只是可以理解为什么 Hash::check 与用户输入与从数据库中获取的密码不匹配。
  • 是的,我说这很可能是因为您没有将哈希正确存储在数据库中。
  • 更不用说您正在检查您提供的代码中的字符串“密码”......
  • @miken32 感谢您的帮助,实际上我正在正确收集它,dd() 正在提取散列字符串和用户输入。我用我正在使用的一些 dd() 检查做了一个小的代码编辑。

标签: laravel hash eloquent


【解决方案1】:

您应该将用户输入密码传递给第一个参数,并将哈希密码从 DB 传递给 Hash::check 的第二个参数,如下所示:

/**
 * Handle an authentication attempt.
 *
 * @param  \Illuminate\Http\Request $request
 *
 * @return Response
 */
public function authLogin(Request $request)
{
    $data = Hause_users::where('username', '=', request->username)->first();

    if (Hash::check($request->password, $data->password)) {
        return 'welcome';
    } else {
        return 'Bye';
    }
}

请参阅Laravel docs 了解更多信息。

【讨论】:

  • 不,这样不行。除了你的 :: where 子句是错误的。我需要散列或从blade.template获取任何东西
  • @NikolaySabev 你的意思是它不起作用?试试吧。
  • 我做了,但仍然相同的旧结果条件总是跳转到 false
【解决方案2】:

检查方法是比较散列密码和输入的密码,你应该这样使用它

$check = Hash::check($request->password,auth()->user()->password);
if($check)
{
// your condition
}
else{
// return what ever you want
}

【讨论】:

  • 不,这没有用,在我使用 if 条件的地方没有区别
【解决方案3】:

我发现了问题,它在我的刀片模板中。我有一些错误检查 {{old 'password'}} 导致不正确的字符串与输入冲突。当我删除那部分 value="{{ old('password') }} 时,一切都很顺利。

【讨论】:

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