【问题标题】:How to compare new password to old hashed password?如何将新密码与旧哈希密码进行比较?
【发布时间】:2021-09-27 04:27:49
【问题描述】:

我在 laravel 8 中更新了当前密码 API。如果我将新密码作为参数传递,它正在更改我的当前密码。但问题是如果我同时传递两个密码,即旧密码和新密码相同。它仍然说密码已更新。我想显示一条错误消息,说明密码不应该相同。这是我更改密码的代码:

public function updatePassword(Request $request)
{
    $input = $request->validate([
        'old_password'=> 'string',
        'password'=> 'string'
    ]);
    if (Hash::check($input['old_password'], auth()->user()->password)) {
        $user = new User();
        $user = auth()->user();
        $user->password = bcrypt($input['password']);
        $user->save();

        return $this->success('Password Updated', $user);
    }
    return $this->error('Password should not be same');
}`

【问题讨论】:

标签: php laravel api passwords


【解决方案1】:

您已经输入了旧密码。

if($input['password'] === $input['old_password']){
    return $this->error('Password should not be same');
}

【讨论】:

  • 它不会将我的旧密码与我的数据库中存储的密码匹配
  • 如果它与旧密码不匹配 - 你必须抛出未经授权的异常 - 使用 auth()->validate(['id' => auth()->id, 'password' => $input['old_password']])
猜你喜欢
  • 1970-01-01
  • 2019-07-02
  • 2023-03-29
  • 2012-04-01
  • 2017-08-05
  • 1970-01-01
  • 2016-03-06
  • 2021-10-23
  • 1970-01-01
相关资源
最近更新 更多