我正在训练在Laravel 10项目中判断密码验证,但函数"check"返回假,而且,我可以使用相同的密码登录.我不知道为什么返回假.

谢谢.

    public function recharge_by_client(Request $request){

        $telf      = $request->telf;
        $password  = $request->password;
        $credit    = $request->credit;

        try {
            $client = Client::where('telf', $telf)->first();

            //if verification data is false, returning 0. 
            if ($client && Hash::check($password, $client->password)) {
                \Log::info('Entramos a update');
                HomeHelper::recharge_money($client->id, $credit);
            }else{
                \Log::info('NO entramos a update');
                return ['credit_updated' => 0];
            }
        } catch (\Exception $e) {
            \Log::info($e->getMessage());
            //A exception has ocurre, returning 0.
            return ['credit_updated' => 0];
        }

        //Returning current credit.
        return ['credit_updated' => Client::find($client->id)->credit];
    }

推荐答案

以下是有助于解决此问题的故障排除步骤.

1 Try a Test with Known Values:您应该能够通过使用已知值测试Hash::check()来重现此结果. 使用测试路径,您可以手动哈希密码,然后try 使用相同的密码判断它,看看哈希::check()是否仍然返回true.这里有一个可以帮助解决这个问题的示例代码.

$knownPassword = 'password123';
$hashed = Hash::make($knownPassword);

if (Hash::check($knownPassword, $hashed)) {
    echo 'Password check succeeded.';
} else {
    echo 'Password check failed.';
}

  1. Hash Format Consistency:判断您的数据库以确保哈希格式正确存储并且没有被更改或截断(由于字段大小).理想情况下,我将其设置为varchar(Hash Format Consistency:)或更长

  2. White Spaces or Special Characters:确认密码哈希中没有额外的空白或特殊字符.您是否正在修剪用户请求的输入?

  3. Confirm the user/record information is retrieved: 确认代码(Client::where('telf', $telf)->first())的这一部分确实正在获取正确的记录,您可以运行dd()来确认该记录.

我认为其中之一应该可以帮助您调试和解决这个问题. 欢呼

Laravel相关问答推荐

AJAX响应中未定义的全名

为什么使用 created_at 表在迁移错误中创建表?

如何在 Laravel 9 中获取带分页的订单列表?

自从 Laravel 使用 Vite 更新后,我无法运行npm run dev

Laravel Blade:@stop VS @show VS @endsection VS @append

判断 Laravel 模型表中是否存在列,然后应用条件

路由中间的可选参数

如何修复无效请求(不支持的 SSL 请求)

Laravel Eloquent:访问属性和动态表名

Laravel 5.4 LengthAwarePaginator

Laravel - 超过 PHP 最大上传大小限制时验证文件大小

Laravel 扩展验证自定义消息

Laravel Electron邮件验证模板位置

错误:您的要求无法解析为一组可安装的软件包.

数组的旧输入?

如何重定向到laravel上的公共文件夹

Laravel 5 事件处理程序未触发

如何使用 Laravel 迁移在 Mysql 列中添加注释

开发中的 Laravel 和视图缓存 - 无法立即看到更改

带有 Xdebug 的 Laravel 5 总是抛出The payload is invalid..