首页 文章

哈希::检查不返回适当的结果

提问于
浏览
1

我有一个 users 表,用于存储注册用户' username, email, password . 密码在保存到各自的表记录之前通过 Hash::make() 函数进行哈希处理 .

我正在尝试从头开始手动创建 AuthController 模块 .

在我的 authenticate() 函数中,我正在检查尝试登录的用户输入的密码是否与用户名对应的哈希密码匹配,如下所示:

public function authenticate(AuthRequest $request)
{
    $hashedPassword = User::where('name', $request->name)
        ->select('password')
        ->get();

    $auth = Hash::check($request->password, $hashedPassword);

    //Die dumping just to check if the hash check has worked
    dd($auth);
}

这里, AuthRequest 是Request类,它在将 $request 变量传递给 authenticate() 函数之前验证表单输入 .

现在,不幸的是, Hash::check() 函数没有按预期响应 . 如上所述,我试图验证用户在登录表单中输入的密码是否与数据库表中的散列密码相匹配 . 但是,尽管输入了与散列密码相同的密码, $auth 变量仍会收到 false 值 .

2 回答

  • 2

    get 返回一个集合 . 要直接获取值,请使用 value 方法:

    $hashedPassword = User::where($request->only('name'))->value('password');
    

    阅读the docs, Headers 为 Retrieving A Single Row / Column From A Table .


    P.S. 如果您使用的是旧版本的Laravel,则该方法称为 pluck .

  • 2

    get更改为first以返回第一个项目而不是集合

    $user = User::where('name', $request->name)
        ->select('password')
        ->first();
    
    $hashedPassword = $user->password;
    

相关问题