validatePassword method

Future<PasswordValidationStatus> validatePassword(
  1. FirebaseAuth auth,
  2. String? password
)

Validates a password against the password policy configured for the project or tenant.

If no tenant ID is set on the Auth instance, then this method will use the password policy configured for the project. Otherwise, this method will use the policy configured for the tenant. If a password policy has not been configured, then the default policy configured for all projects will be used.

If an auth flow fails because a submitted password does not meet the password policy requirements and this method has previously been called, then this method will use the most recent policy available when called again.

Returns a PasswordValidationStatus with the following fields:

  • isValid: A boolean indicating if the password is valid.
  • passwordPolicy: The password policy used to validate the password.
  • meetsMinPasswordLength: A boolean indicating if the password meets the minimum length requirement.
  • meetsMaxPasswordLength: A boolean indicating if the password meets the maximum length requirement.
  • meetsLowercaseRequirement: A boolean indicating if the password meets the lowercase requirement.
  • meetsUppercaseRequirement: A boolean indicating if the password meets the uppercase requirement.
  • meetsDigitsRequirement: A boolean indicating if the password meets the digits requirement.
  • meetsSymbolsRequirement: A boolean indicating if the password meets the symbols requirement.

A FirebaseAuthException maybe thrown with the following error code:

  • invalid-password:
  • Thrown if the password is null or empty.

An Exception is thrown if the password policy cannot be fetched, for example because of a network error.

Implementation

Future<PasswordValidationStatus> validatePassword(
  FirebaseAuth auth,
  String? password,
) async {
  if (password == null || password.isEmpty) {
    throw FirebaseAuthException(
      code: 'invalid-password',
      message: 'Password cannot be null or empty',
    );
  }
  PasswordPolicyApi passwordPolicyApi = PasswordPolicyApi(
    auth.app.options.apiKey,
  );
  PasswordPolicy passwordPolicy = await passwordPolicyApi
      .fetchPasswordPolicy();
  PasswordPolicyImpl passwordPolicyImpl = PasswordPolicyImpl(passwordPolicy);
  return passwordPolicyImpl.isPasswordValid(password);
}