【问题标题】:Check whether password is correct or not in Laravel在 Laravel 中检查密码是否正确
【发布时间】:2016-11-25 21:22:57
【问题描述】:

在 laravel 中,我想检查用户是否输入当前密码,而不是检查存储在该用户数据中的数据库中的密码。如果正确则继续,否则给出消息密码不正确。

我是 laravel 的新手,所以对此没有确切的想法。

提前致谢。

$('#pwd').blur(function(){
            var oldpwd=$('#pwd').val();
            var uid = $('#id').val();
            console.log(uid);
            if(oldpwd != "")
            {
              $.ajax({
                  url : "{{ url/profile/checkOldPwd}}",
                  data : { oldpwd:oldpwd , uid:uid },
                  type : "POST",
                  success : function(data) {
                    if(data == 0){
                      $("#msg-old").show();
                      $("#msg-old").html("Password is Incorrect!");
                      $("#pwd").val("");
                      $("#pwd").focus();
                  }
                  else
                  {
                    $("#msg-old").hide();
                    $("#msg-old").html("");
                  }
                }
                });
              }
            });

【问题讨论】:

  • 我只是尝试使用 ajax,但不知道如何检查,所以你能提供一些想法吗?
  • 查看documentation link 以获取有关授权的更多信息或向我们展示您的代码以帮助您。
  • 以上是我的ajax代码。

标签: laravel


【解决方案1】:

正如 Hiren 所提到的,您可以使用默认注册的哈希,因为它被传递给所使用的特定 UserProvider。默认为Illuminate\Hashing\BcryptHasher

您可以通过以下几种方式使用它:

  1. 从容器中取出
$user = User::find($id);
$hasher = app('hash');
if ($hasher->check('passwordToCheck', $user->password)) {
    // Success
}
  1. 使用外观
$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) {
    // Success
}
  1. 出于兴趣,使用通用 php 函数 password_verify 也可以。然而,这是可行的,因为它使用的默认哈希算法是 bcrypt。
if (password_verify('passwordToCheck', $user->password)) {
    // Success
}

【讨论】:

  • 你更喜欢哪个?容器外是最优雅/最快的吗?
  • 我个人越来越喜欢容器外(1)
  • @LeonVismer 如果存储在数据库中的现有密码哈希需要重新哈希怎么办?
  • @KiranManiya 如果调整了工作系数,可以使用Hash:needsRehash($hashed) 进行检查。
  • @LeonVismer 是的,我明白了。我也更新了答案。
【解决方案2】:

你可以使用 hash:check 方法。

使用哈希创建密码:

$password = Hash::make('secret');

检查密码:

if (Hash::check('secret', $hashedPassword))
{
    // The passwords match...
}

【讨论】:

    【解决方案3】:

    当用户尝试访问该页面时,将他们重定向到授权页面。

    执行 ajax 调用,然后在您的 php 中执行以下操作:

    public function check(Request $request)
    {
        if(Hash::check($request->password, $user->password)) {
            // They match
        } else {
            // They don't match
        }
    }
    

    我没有对此进行测试,所以它可能不起作用。

    【讨论】:

    • 这行不通。 Bcrypt每次返回不同的字符串,不像md5
    • @Dastur 谢谢你uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu????????????????????
    【解决方案4】:

    去做吧

    public function authenticateEmployee(array $data){
    
     $email = $data['email'];
     $password = $data['password'];
    
     $user = User::where('email', '=', $email)->first();   //get db User data   
     if(Hash::check($password, $user->password)) {   
    
          return response()->json(['status'=>'false','message'=>'password is correct']);
    
        } else {
            return 'false';
        }
    
    }
    

    【讨论】:

    • 也许收到$data->get('email') 这样的电子邮件而不是$data['email'] 是更好的做法?
    【解决方案5】:

    if (Hash::check($request->password, Auth::user()->password)) { //Sucess }

    【讨论】:

      【解决方案6】:

      哇,其实超级简单:

      $request->validate([
           'password' => 'required|string|password',
      ]);
      

      验证字段必须与已验证用户的密码匹配。

      但是对于未来的人来说:

      此规则已重命名为 current_password,目的是在 Laravel 9 中将其删除。请改用当前密码规则。

      原来如此

      $request->validate([
           'password' => 'required|string|current_password',
      ]);
      

      Source

      【讨论】:

        【解决方案7】:

        laravel 6.* 中的认证逻辑在“vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php”中。 在此文件中,您将找到 sendFailedLoginResponse() 方法,我们将在 LoginController.php 上覆盖该方法

        protected function sendFailedLoginResponse(Request $request)
            {
        
                if (!User::where('email', $request->email)->first()) {
                    throw ValidationException::withMessages([trans('auth.email')]);
                }
                
                if (!User::where('email', $request->email)->where('password', bcrypt($request->password))->first()) {
                    throw ValidationException::withMessages([trans('auth.password')]);
                }
            }
        

        此自定义方法验证用户是否存在于数据库中,查找您在模型上设置的唯一值。如果用户存在于数据库中,则检查密码。然后,您可以使用

        调用错误
        @if($errors->any())
        <div class="alert alert-danger" >
          <ul>
            @foreach($errors->all() as $error)
              <li>{{ $error }}</li>
            @endforeach
          </ul>
        
        </div>
        
        @endif
        

        在您的刀片文件上。请记住,您从“resources\lang\en\auth.php”中调用这些 auth.* 文件

        【讨论】:

          【解决方案8】:
          1. 使用您的 AJAX 调用访问服务器路由。
          2. 检查auth()-&gt;attempt($request-&gt;only('email', 'password'))的结果
          3. 返回带有消息和 HTTP 状态代码的 JSON(如果成功则返回 200,如果失败则返回 400)。
          4. success() 方法中的消息显示到一个div。如果是错误,则将error() 方法内的错误消息显示给一个div。

          【讨论】:

            猜你喜欢
            • 2023-03-17
            • 2015-12-28
            • 1970-01-01
            • 2017-07-24
            • 1970-01-01
            • 2016-09-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多