registerUserWithEmailAndPassword method

  1. @override
Future<Either<AuthServiceSignInFailure, Unit>> registerUserWithEmailAndPassword(
  1. String email,
  2. String password
)
override

Implementation

@override
Future<Either<AuthServiceSignInFailure, Unit>> registerUserWithEmailAndPassword(
    String email, String password) async {
  try {
    // Call the function to create the auth user and db user
    var result = await authCallable.call({
      'action': 'authRegisterWithEmailAndPassword',
      'email': email,
      'password': password,
    });

    logd('Register result: ${result.data}');

    // // Create the auth user
    // fb_auth.UserCredential userCred =
    //     await _auth.createUserWithEmailAndPassword(email: email, password: password);

    // if (userCred.user == null) {
    //   return left(AuthServiceSignInFailure.unexpected);
    // }

    // // Create, Get the firebase user and return
    // var dbCreateResult = await Get.find<UserRepositoryInt>().createUserIfNotExist(User(
    //   id: userCred.user!.uid,
    //   email: email,
    // ));

    // // Determine outcome
    // Either<AuthServiceSignInFailure, User> returnVal = dbCreateResult.fold(
    //   (failure) {
    //     return failure.maybeWhen(
    //       orElse: () => left(AuthServiceSignInFailure.databaseError()),
    //     );
    //   },
    //   (success) {
    //     currentUser.value = success;
    //     return right(success);
    //   },
    // );

    // // Finally return
    // return returnVal;

    // Sometimes we can't sign in right away after creating the account
    const int maxRetries = 5;
    int i = 0;
    for (i; i < maxRetries; i++) {
      try {
        await _fbAuth.signInWithEmailAndPassword(email: email, password: password);
      } on fb_auth.FirebaseAuthException catch (f) {
        if (f.code == 'user-not-found') {
          logd('AUTH SIGN IN FAILED. Waiting a second then trying again');
          await Future.delayed(signInRetryDelay);
          continue;
        } else {
          loge(f);
          return left(AuthServiceSignInFailure.unexpected);
        }
      }
      break;
    }
    if (i >= maxRetries) {
      return left(AuthServiceSignInFailure.signInTimedOut);
    }

    return right(unit);
  } on FirebaseFunctionsException catch (e) {
    logd('registerUserWithEmailAndPassword FirebaseFunctionsException: ${e.message}');
    switch (e.message) {
      case 'email-already-in-use':
        return left(AuthServiceSignInFailure.userAlreadyExists);
      case 'weak-password':
        return left(AuthServiceSignInFailure.weakPassword);
      case 'invalid-email':
        return left(AuthServiceSignInFailure.invalidEmail);
      default:
        return left(AuthServiceSignInFailure.unexpected);
    }
  } catch (e) {
    logd('registerUserWithEmailAndPassword other exception');
    loge(e);
    return left(AuthServiceSignInFailure.unexpected);
  }
}