createUser method

Future<AdminUser> createUser({
  1. String? uid,
  2. String? email,
  3. String? password,
  4. bool emailVerified = false,
  5. String? displayName,
  6. String? photoUrl,
  7. String? phoneNumber,
  8. bool disabled = false,
  9. Map<String, Object?>? customClaims,
})

Creates a user and returns it as stored.

Without uid the backend picks one. password needs at least 6 characters, phoneNumber the E.164 format (+15555550100). customClaims are set by a second request (setCustomUserClaims), checked before the user is created; if that request fails the user is deleted again and the error is thrown.

Throws uid-already-exists, email-already-exists, phone-number-already-exists, and invalid-uid, invalid-email, invalid-password, invalid-phone-number, invalid-photo-url or invalid-display-name for an argument refused before the request.

Implementation

Future<AdminUser> createUser({
  String? uid,
  String? email,
  String? password,
  bool emailVerified = false,
  String? displayName,
  String? photoUrl,
  String? phoneNumber,
  bool disabled = false,
  Map<String, Object?>? customClaims,
}) async {
  _checkOpen();
  if (uid != null) {
    _checkUid(uid);
  }
  _checkProperties(
    email: email,
    password: password,
    displayName: displayName,
    photoUrl: photoUrl,
    phoneNumber: phoneNumber,
  );
  final String? encodedClaims = customClaims == null
      ? null
      : _checkUserClaims(customClaims);
  final Map<String, Object?> response =
      await _identityToolkit('accounts', <String, Object?>{
        'localId': ?uid,
        'email': ?email,
        'password': ?password,
        'emailVerified': emailVerified,
        'displayName': ?displayName,
        'photoUrl': ?photoUrl,
        'phoneNumber': ?phoneNumber,
        'disabled': disabled,
      });
  final Object? created = response['localId'];
  if (created is! String || created.isEmpty) {
    throw const FirebaseAuthAdminException(
      code: 'internal-error',
      message: 'The backend did not return the uid of the new user.',
    );
  }
  if (encodedClaims != null) {
    try {
      await _identityToolkit('accounts:update', <String, Object?>{
        'localId': created,
        'customAttributes': encodedClaims,
      });
    } on Object {
      try {
        await _identityToolkit('accounts:delete', <String, Object?>{
          'localId': created,
        });
      } on Object {
        // The error of the claims is the one to report.
      }
      rethrow;
    }
  }
  return getUser(created);
}