diff --git a/src/Auth.php b/src/Auth.php index cdb1d9a..16d96e0 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -1204,6 +1204,40 @@ final class Auth extends UserManager { } } + /** + * Check if the supplied selector/token pair can be used to reset a password + * + * The password can be reset using the supplied information if this method does *not* throw any exception + * + * The selector/token pair must have been generated previously by calling `Auth#forgotPassword(...)` + * + * @param string $selector the selector from the selector/token pair + * @param string $token the token from the selector/token pair + * @throws InvalidSelectorTokenPairException if either the selector or the token was not correct + * @throws TokenExpiredException if the token has already expired + * @throws ResetDisabledException if the user has explicitly disabled password resets for their account + * @throws TooManyRequestsException if the number of allowed attempts/requests has been exceeded + * @throws AuthError if an internal problem occurred (do *not* catch) + */ + public function canResetPasswordOrThrow($selector, $token) { + try { + // pass an invalid password intentionally to force an expected error + $this->resetPassword($selector, $token, null); + + // we should already be in one of the `catch` blocks now so this is not expected + throw new AuthError(); + } + // if the password is the only thing that's invalid + catch (InvalidPasswordException $ignored) { + // the password can be reset + } + // if some other things failed (as well) + catch (AuthException $e) { + // re-throw the exception + throw $e; + } + } + /** * Check if the supplied selector/token pair can be used to reset a password *