I want verify the existing password for a user (to allow them to change their password).
I thought to go the following route but ran into the problem that the hashed password always shows up as a different hash. I am using UserPasswordHasherInterface.
$hashedOldPassword = $passwordHasher->hashPassword(
$user,
$data['oldPassword']
);
if ($hashedOldPassword === $user->getPassword()) {
setNewPassword();
}
To verify a password you do not rehash it. Each time you call
hashPassword()you'll get a different hash, because the hashing algorithm introduces a random salt for security.But that interface includes a much more convenient
isPasswordValid()method.So simply do: