【问题标题】:How to properly update password in Laravel?如何在 Laravel 中正确更新密码?
【发布时间】:2018-06-09 22:44:59
【问题描述】:

我有一个使用自定义用户身份验证的 Laravel 应用程序。现在我正在尝试允许用户更新他们的密码,我遇到的问题是我需要检查旧密码,如果该字段不为空,而不是我想检查新密码是否与密码匹配重复提交,如果它更新它是我在控制器中的代码:

$newpass = $request->input('password');
$passrepeat = $request->input('passwordRepeat');

if ($oldPass = $request->input('oldPassword') != null)
{
  if (Hash::check($request->oldPassword, auth()->user()->password)){
       if ($newpass = $passrepeat){

                 $password = bcrypt($request['password']);
                 $user->password = $password;

       }
  }
}

然后我更新它:

$user->update();

但我的代码似乎甚至没有检查密码字段。当我检查 oldPassword 字段时,我显然犯了错误。我可以尝试的选项太多了,所以我决定提出问题。

Laravel 5.4

【问题讨论】:

  • 是的,这是一个很好的例子,但有一个问题,在我的情况下,输入字段更多,用户可能会完全跳过更新密码,但如果它决定更新它而不是我 ckec 为 oldPassword 提交如果它已填写并与旧密码匹配,那么我会重置它

标签: php laravel laravel-5.4


【解决方案1】:

来自the docs

check 方法允许您验证给定的纯文本字符串是否对应于给定的哈希值。但是,如果你使用 Laravel 中包含的 LoginController,你可能不需要直接使用它,因为这个控制器会自动调用这个方法:

if (Hash::check('plain-text', $hashedPassword)) {

https://laravel.com/docs/5.5/hashing#basic-usage

【讨论】:

  • 我可以用 Hash::check 做到这一点吗? if (Hash::check($request->input('oldPassword', Auth::User()->getAuthPassword() ) )){
  • @Nikonah if (Hash::check($request->oldPassword), auth()->user()->password))
  • 那行得通。谢谢您的帮助。顺便说一句,您在该评论中犯了拼写错误,应该是if (Hash::check($request->oldPassword, auth()->user()->password))
  • 不,有问题。它只是通过了那个阶段并更新了密码,奇怪的是它甚至不检查新密码和重复匹配。我正在更新问题的外观。
【解决方案2】:

只需替换这两行

  $password = bcrypt($request['password']);
  $user->password = $password;

 $user->password = password_hash($request->password, PASSWORD_DEFAULT);

【讨论】:

  • 它仍然没有解决旧密码检查问题。这行if ($oldPass = $request->input('oldPassword') != null) 只是跳过并进行更新
猜你喜欢
  • 1970-01-01
  • 2020-12-04
  • 2019-03-10
  • 2016-11-25
  • 2022-04-14
  • 1970-01-01
  • 2018-01-04
  • 2016-04-30
  • 2018-11-27
相关资源
最近更新 更多