UserRecord.fromResponse constructor

  1. @internal
UserRecord.fromResponse(
  1. GoogleCloudIdentitytoolkitV1UserInfo response
)

Implementation

@internal
factory UserRecord.fromResponse(
  auth1.GoogleCloudIdentitytoolkitV1UserInfo response,
) {
  final localId = response.localId;
  // The Firebase user id is required.
  if (localId == null) {
    throw FirebaseAuthAdminException(
      AuthClientErrorCode.internalError,
      'INTERNAL ASSERT FAILED: Invalid user response',
    );
  }
  // If disabled is not provided, the account is enabled by default.
  final disabled = response.disabled ?? false;
  final metadata = UserMetadata.fromResponse(response);

  final providerData = <UserInfo>[];
  final providerUserInfo = response.providerUserInfo;
  if (providerUserInfo != null) {
    for (final entry in providerUserInfo) {
      providerData.add(UserInfo.fromResponse(entry));
    }
  }

  // If the password hash is redacted (probably due to missing permissions)
  // then clear it out, similar to how the salt is returned. (Otherwise, it
  // *looks* like a b64-encoded hash is present, which is confusing.)
  final passwordHash =
      response.passwordHash == _b64Redacted ? null : response.passwordHash;

  final customAttributes = response.customAttributes;
  final customClaims = customAttributes != null
      ? UnmodifiableMapView(
          jsonDecode(customAttributes) as Map<String, Object?>,
        )
      : null;

  DateTime? tokensValidAfterTime;
  final validSince = response.validSince;
  if (validSince != null) {
    // Convert validSince first to UTC milliseconds and then to UTC date string.
    tokensValidAfterTime = DateTime.fromMillisecondsSinceEpoch(
      // TODO double check that 1000
      int.parse(validSince) * 1000,
      isUtc: true,
    );
  }

  MultiFactorSettings? multiFactor =
      MultiFactorSettings.fromResponse(response);
  if (multiFactor.enrolledFactors.isEmpty) {
    multiFactor = null;
  }

  return UserRecord(
    uid: localId,
    email: response.email,
    emailVerified: response.emailVerified ?? false,
    displayName: response.displayName,
    photoUrl: response.photoUrl,
    phoneNumber: response.phoneNumber,
    disabled: disabled,
    metadata: metadata,
    providerData: UnmodifiableListView(providerData),
    passwordHash: passwordHash,
    passwordSalt: response.salt,
    customClaims: customClaims,
    tenantId: response.tenantId,
    tokensValidAfterTime: tokensValidAfterTime,
    multiFactor: multiFactor,
  );
}