It is possible to check the user's password with Apex through the UserManagement
class:
Auth.VerificationResult res = UserManagement.verifyVerificationMethod(
null,
'the actual password',
Auth.VerificationMethod.PASSWORD
);
System.debug(res);
The output is a Auth.VerificationResult object in which you can check the message and the success:
16:50:55:055 USER_DEBUG [6]|DEBUG|VerificationResult:[message=FailedInvalidPassword, redirect=null, success=false]
If the success attribute returns true
, then the password matches.
Site.login()
might be more appropriate. It should be incredibly rare to need to actually check a user's password.