检查Laravel中的密码是否正确

Kim*_*nes 11 laravel

在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("");
                  }
                }
                });
              }
            });
Run Code Online (Sandbox Code Playgroud)

Leo*_*mer 35

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

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

  1. 走出容器
$user = User::find($id);
$hasher = app('hash');
if ($hasher->check('passwordToCheck', $user->password)) {
    // Success
}
Run Code Online (Sandbox Code Playgroud)
  1. 使用Facade
$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) {
    // Success
}
Run Code Online (Sandbox Code Playgroud)
  1. 出于兴趣使用通用的PHP功能password_verify也有效.但是这可行,因为它使用的默认哈希算法是bcrypt.
if (password_verify('passwordToCheck', $user->password)) {
    // Success
}
Run Code Online (Sandbox Code Playgroud)


Das*_*tur 5

当用户尝试访问该页面时,将其重定向到身份验证页面。

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

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

我还没有测试过这个,所以它可能不起作用。


Hir*_*ana 5

您可以使用hash:check方法。

使用哈希创建密码:

$password = Hash::make('secret');
Run Code Online (Sandbox Code Playgroud)

检查密码:

if (Hash::check('secret', $hashedPassword))
{
    // The passwords match...
}
Run Code Online (Sandbox Code Playgroud)