updateUser method

Future<UserRecord> updateUser(
  1. String uid,
  2. UpdateRequest properties
)
inherited

Updates an existing user.

See https://firebase.google.com/docs/auth/admin/manage-users#update_a_user for code samples and detailed documentation.

  • uid - The uid corresponding to the user to update.
  • properties - The properties to update on the provided user.

Returns a Future fulfilled with the updated user data.

Implementation

Future<UserRecord> updateUser(String uid, UpdateRequest properties) async {
  // Although we don't really advertise it, we want to also handle linking of
  // non-federated idps with this call. So if we detect one of them, we'll
  // adjust the properties parameter appropriately. This *does* imply that a
  // conflict could arise, e.g. if the user provides a phoneNumber property,
  // but also provides a providerToLink with a 'phone' provider id. In that
  // case, we'll throw an error.
  var request = properties;
  final providerToLink = properties.providerToLink;
  switch (providerToLink) {
    case UserProvider(providerId: 'email'):
      if (properties.email != null) {
        throw FirebaseAuthAdminException(
          AuthClientErrorCode.invalidArgument,
          "Both UpdateRequest.email and UpdateRequest.providerToLink.providerId='email' were set. To "
          'link to the email/password provider, only specify the UpdateRequest.email field.',
        );
      }
      request = properties.copyWith(email: providerToLink.uid);
    case UserProvider(providerId: 'phone'):
      if (properties.phoneNumber != null) {
        throw FirebaseAuthAdminException(
          AuthClientErrorCode.invalidArgument,
          "Both UpdateRequest.phoneNumber and UpdateRequest.providerToLink.providerId='phone' were set. To "
          'link to a phone provider, only specify the UpdateRequest.phoneNumber field.',
        );
      }
      request = properties.copyWith(phoneNumber: providerToLink.uid);
  }
  final providersToUnlink = properties.providersToUnlink;
  if (providersToUnlink != null && providersToUnlink.contains('phone')) {
    // If we've been told to unlink the phone provider both via setting
    // phoneNumber to null *and* by setting providersToUnlink to include
    // 'phone', then we'll reject that. Though it might also be reasonable
    // to relax this restriction and just unlink it.
    if (properties.phoneNumber == null) {
      throw FirebaseAuthAdminException(
        AuthClientErrorCode.invalidArgument,
        "Both UpdateRequest.phoneNumber and UpdateRequest.providersToUnlink=['phone'] were set. To "
        'unlink the phone provider, only specify the UpdateRequest.providersToUnlink field.',
      );
    }
  }

  final existingUid =
      await _authRequestHandler.updateExistingAccount(uid, request);
  return getUser(existingUid);
}