vibin
vibin

Reputation: 29

how can i check whether the entered password is current password?

To change the password, i enter the current password.For the correct password also an error msg will be displayed

public function passwordupdate(Request $request){

    $user=user::find(Auth::user()->id);

    $validator=Validator::make($request->all(), [
        //'password' => 'required|confirmed|min:6|max:14' 
        'cpassword'=> 'required|confirmed|min:6|max:14'                     
    ]);
    if($validator->fails()){
        return redirect()->back()->withErrors(['cpassword'=>'Please check the password you given']);
    }else{
        $user->cpassword=bcrypt($request->cpassword);
         $user->save();
        return redirect()->back();
    }

}

Upvotes: 0

Views: 90

Answers (3)

Zenel Rrushi
Zenel Rrushi

Reputation: 2366

  1. First you need to use Illuminate\Support\Facades\Hash;

  2. to check if the old password that was inputed by user you can use Hash::check(). This function takes 2 parameters. The first one is the plain string and the second one is the hashed password(old password).

You can retrieve old password by Auth::user()->password so you if condition would be

if(Hash::check($request['oldpass'], $user->password)){
    //insert the new password
    Auth::user->update([
    'password'=>bcrypt($data['newpass'])
    ]);
    Auth::user->save();
}

P.S Hash::check() return true or false

Upvotes: 1

aleksejjj
aleksejjj

Reputation: 1835

Hash checking https://laravel.com/docs/5.3/hashing

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

Upvotes: 3

Alexey Mezenin
Alexey Mezenin

Reputation: 163798

You may use Hash::check() method. For example, if you want to compare entered password with current user's one:

if (Hash::check($request->password, auth()->user()->password) {
    // Entered password is correct.

Upvotes: 0

Related Questions