signUpByUsername method

Future<AuthResponse<T>> signUpByUsername(
  1. UsernameAuthenticator authenticator, {
  2. SignByBiometricCallback<T>? onBiometric,
  3. Object? args,
  4. String? id,
  5. bool notifiable = true,
})

Implementation

Future<AuthResponse<T>> signUpByUsername(
  UsernameAuthenticator authenticator, {
  SignByBiometricCallback<T>? onBiometric,
  Object? args,
  String? id,
  bool notifiable = true,
}) async {
  emit(
    args: args,
    id: id,
    notifiable: notifiable,
    const AuthResponse.loading(Provider.username, AuthType.register),
  );

  try {
    final hasAnonymous = this.hasAnonymous;
    final response = await delegate.signUpWithUsernameNPassword(
      authenticator.username,
      authenticator.password,
    );
    if (!response.isSuccessful) {
      return emit(
        args: args,
        id: id,
        notifiable: notifiable,
        AuthResponse.failure(
          response.error,
          provider: Provider.username,
          type: AuthType.register,
        ),
      );
    }

    final result = response.data;
    if (result == null) {
      return emit(
        args: args,
        id: id,
        notifiable: notifiable,
        AuthResponse.failure(
          msg.authorization,
          provider: Provider.username,
          type: AuthType.register,
        ),
      );
    }

    final creationTime = EntityHelper.generateTimeMills;
    final user = authenticator.update(
      id: Modifier(result.uid),
      anonymous: Modifier(result.isAnonymous),
      email: Modifier(result.email),
      name: Modifier(result.displayName),
      phone: Modifier(result.phoneNumber),
      photo: Modifier(result.photoURL),
      provider: Modifier(Provider.username),
      loggedIn: Modifier(true),
      loggedInTime: Modifier(creationTime),
      timeMills: Modifier(creationTime),
    );

    final value = await _update(
      id: user.id,
      hasAnonymous: hasAnonymous,
      onBiometric: onBiometric,
      initials: user.filtered,
      updates: {
        if (authenticator.extra != null) ...authenticator.extra!,
        AuthKeys.i.loggedIn: true,
        AuthKeys.i.loggedInTime: EntityHelper.generateTimeMills,
        AuthKeys.i.anonymous: result.isAnonymous,
        AuthKeys.i.email: result.email,
        AuthKeys.i.name: result.displayName,
        AuthKeys.i.password: authenticator.password,
        AuthKeys.i.phone: result.phoneNumber,
        AuthKeys.i.photo: result.photoURL,
        AuthKeys.i.provider: Provider.username.id,
        AuthKeys.i.username: authenticator.username,
        AuthKeys.i.verified: result.emailVerified,
      },
    );

    return emit(
      args: args,
      id: id,
      notifiable: notifiable,
      AuthResponse.authenticated(
        value,
        msg: msg.signUpWithUsername.done,
        provider: Provider.username,
        type: AuthType.register,
      ),
    );
  } catch (error) {
    return emit(
      args: args,
      id: id,
      notifiable: notifiable,
      AuthResponse.failure(
        msg.signUpWithUsername.failure ?? error,
        provider: Provider.username,
        type: AuthType.register,
      ),
    );
  }
}