signInWithEmailAndPassword method

Future<UserCredential> signInWithEmailAndPassword(
  1. String email,
  2. String password
)

Attempts to sign in a user with the given email address and password.

If successful, it also signs the user in into the app and updates any authStateChanges, or idTokenChanges stream listeners.

Important: You must enable Email & Password accounts in the Auth section of the Firebase console before being able to use them.

A FirebaseAuthException maybe thrown with the following error code:

  • invalid-email
    • Thrown if the email address is not valid.
  • user-disabled
    • Thrown if the user corresponding to the given email has been disabled.
  • user-not-found
    • Thrown if there is no user corresponding to the given email.
  • wrong-password
    • Thrown if the password is invalid for the given email, or the account corresponding to the email does not have a password set.

Implementation

Future<UserCredential> signInWithEmailAndPassword(
    String email, String password) async {
  try {
    final response = await _api.emailAndPasswordAuth
        .signInWithEmailAndPassword(email, password);
    final userData = await _api.userAccount.getAccountInfo(response.idToken!);

    // Map the json response to an actual user.
    final user = User(userData..addAll(response.toJson()), this);

    _updateCurrentUserAndEvents(user, true);

    // Make a credential object based on the current sign-in method.
    return UserCredential._(
      auth: this,
      credential:
          EmailAuthProvider.credential(email: email, password: password),
      additionalUserInfo: AdditionalUserInfo(
        isNewUser: false,
        providerId: EmailAuthProvider.PROVIDER_ID,
        username: userData['screenName'],
        profile: {
          'displayName': userData['displayName'],
          'photoUrl': userData['photoUrl']
        },
      ),
    );
  } catch (e) {
    rethrow;
  }
}