How to Validate Current Password in Laravel
In this lesson, we will see how to validate the current password in Laravel, we will see two examples the first using the Hash check method and the second using a validation rule.
Validate current password using Hash::check method
In this example we will validate the current password using the Hash::check() method this method takes 2 params the first is the current password provided by the user and the second is the stored password to compare with.
use Illuminate\Support\Facades\Hash;
public function auth(Request $request)
{
$request->validate([
'email' => ['required', 'email'],
'current_password' => ['required'],
]);
$user = User::where('email', $request->email)->first();
if (Hash::check($request->current_password, $user->password)) {
//code
}
}
Validate current password using a rule
In this example we will validate the current password using the rule 'current_password' this rule checks that the current password provided by the user matches the authenticated user's password.
$request->validate([
'current_password' => ['required', 'current_password']
]);