createUserWithEmailAndPassword method

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

Tries to create a new user account with the given email address and password.

A FirebaseAuthException maybe thrown with the following error code:

  • email-already-in-use
    • Thrown if there already exists an account with the given email address.
  • invalid-email
    • Thrown if the email address is not valid.
  • operation-not-allowed
    • Thrown if email/password accounts are not enabled. Enable email/password accounts in the Firebase Console, under the Auth tab.
  • weak-password
    • Thrown if the password is not strong enough.

Implementation

Future<UserCredential> createUserWithEmailAndPassword(
    String email, String password) async {
  try {
    final response = await _api.emailAndPasswordAuth
        .createUserWithEmailAndPassword(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);

    return UserCredential._(
      auth: this,
      credential:
          EmailAuthProvider.credential(email: email, password: password),
      additionalUserInfo: AdditionalUserInfo(
        isNewUser: true,
        providerId: EmailAuthProvider.PROVIDER_ID,
        username: userData['screenName'],
        profile: {
          'displayName': userData['displayName'],
          'photoUrl': userData['photoUrl']
        },
      ),
    );
  } catch (e) {
    rethrow;
  }
}