Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether password is correct or not in Laravel

Tags:

laravel

In laravel I want to check if user enter current password than check with that password which is store in database in that user data. If correct than continue otherwise give message Password is incorrect.

I am new in laravel so not getting exact idea for this.

Thanks in advance.

$('#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("");
                  }
                }
                });
              }
            });
like image 821
Kim Jones Avatar asked Jul 22 '16 05:07

Kim Jones


People also ask

How can I get laravel password?

You cannot decrypt laravel password hash which is bcrypt. You can change it with new password. You can get the hashedPassword like this: $hashedPassword = Auth::user()->getAuthPassword();

What is my new password and confirm password laravel?

In your form. blade, ensure that the password input field has a name attribute of name="password" -> it has to be password , Also, add an attribute of name="password_confirmation" to the Confirm Password text input box, and it will work.


2 Answers

As Hiren has mentioned you can use the default registered hasher as that is passed to the specific UserProvider used. The default is Illuminate\Hashing\BcryptHasher.

You can use it a couple of ways:

  1. Out of the container
$user = User::find($id);
$hasher = app('hash');
if ($hasher->check('passwordToCheck', $user->password)) {
    // Success
}
  1. Using the Facade
$user = User::find($id);
if (Hash::check('passwordToCheck', $user->password)) {
    // Success
}
  1. Out of interest using the generic php function password_verify also works. However that works because the default hashing algorithm it uses is bcrypt.
if (password_verify('passwordToCheck', $user->password)) {
    // Success
}
like image 70
Leon Vismer Avatar answered Oct 11 '22 08:10

Leon Vismer


you can use hash:check method.

create password using hash:

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

check password:

if (Hash::check('secret', $hashedPassword))
{
    // The passwords match...
}
like image 5
Hiren Makwana Avatar answered Oct 11 '22 09:10

Hiren Makwana