signUpByUsername method

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

Implementation

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

  try {
    final response = await authHandler.signUpWithUsernameNPassword(
      username: authenticator.username,
      password: authenticator.password,
    );
    if (!response.isSuccessful) {
      return emit(
        args: args,
        id: id,
        notifiable: notifiable,
        AuthResponse.failure(
          response.exception,
          provider: AuthProviders.username,
          type: AuthType.register,
        ),
      );
    }

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

    final creationTime = Entity.generateTimeMills;
    final user = authenticator.copy(
      id: result.uid,
      email: result.email,
      name: result.displayName,
      phone: result.phoneNumber,
      photo: result.photoURL,
      provider: AuthProviders.username.name,
      loggedIn: true,
      loggedInTime: creationTime,
      timeMills: creationTime,
    );

    BiometricStatus? biometric;
    if (onBiometric != null) biometric = await onBiometric(user.mBiometric);

    final value = await _update(
      id: user.id,
      initials:
          (biometric != null ? user.copy(biometric: biometric.name) : user)
              .source,
    );

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