Laravel 5.3 – validate new password with old password using hash check

Laravel 5.3 – validate new password with old password using hash check

In this post we will give you information about Laravel 5.3 – validate new password with old password using hash check. Hear we will give you detail about Laravel 5.3 – validate new password with old password using hash checkAnd how to use it also give you demo for it if it is necessary.

In this post, i will tell you how to validate password with database password using Hash::check method.

For security reasons, Laravel provides Hash facade to store your password in secure Bcrypt hashing.

So while you are going to change your password then it first validate with database password using Hash::check if match then you update your password with new password.

Make sure old password should be generated by Hash::make.

For security, you must define in your form to type the password twice to confirm it.

Larave have so many validation rules and as you will see in below example i have validated confirm password must match with new password.

In this example, i have created a panel where user will enter their old password with new password to update their password.

There will be three input field in form :

  • old password
  • new password
  • confirm password
  1. public functionupdatePassword(Request $request)
  2. {
  3.     $this->validate($request,[
  4.         'old_password'=>'required',
  5. 'new_password'=>'required|min:6',
  6. 'confirm_password'=>'required|same:new_password',
  7. ]);
  8. $data=$request->all();
  9. $user= User::find(auth()->user()->id);
  10. if(!Hash::check($data['old_password'],$user->password)){
  11.     returnback()
  12.          ->with('error','The specified password does not match the database password');
  13. }else{
  14. // write code to update password
  15. }
  16. }
public function updatePassword(Request $request)
{
  $this->validate($request, [
    'old_password'     => 'required',
        'new_password'     => 'required|min:6',
        'confirm_password' => 'required|same:new_password',
    ]);

    $data = $request->all();
 
    $user = User::find(auth()->user()->id);

    if(!Hash::check($data['old_password'], $user->password)){
      return back()
                ->with('error','The specified password does not match the database password');
    }else{
       // write code to update password
    }
}


Using Hash::check method, you can verify your plain text string with hash password stored in database.

Hope this code and post will helped you for implement Laravel 5.3 – validate new password with old password using hash check. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

12  +    =  14

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US