registerUserWithJustEmail method

  1. @override
Future<Either<AuthServiceSignInFailure, Unit>> registerUserWithJustEmail(
  1. String email
)
override

Implementation

@override
Future<Either<AuthServiceSignInFailure, Unit>> registerUserWithJustEmail(String email) async {
  try {
    // Call the function to register the user with a random password

    var result = await authCallable.call({
      'action': 'registerWithJustEmail',
      'email': email,
      //TODO: pass the bundle ID or something like that to know whihc app is calling. Using isForApp is legacy and should be removed after the existing functions are updated
      'isForApp': true,
    });

    if (result.data['result'] == 'exists') {
      return left(AuthServiceSignInFailure.userAlreadyExists);
    }

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

    // Sometimes we can't sign in right away after creating the account
    const int maxRetries = 8;
    int i = 0;
    for (i; i < maxRetries; i++) {
      bool hadError = false;

      try {
        await _fbAuth.signInWithEmailAndPassword(
          email: email,
          password: result.data['password'],
        );
      } on fb_auth.FirebaseAuthException catch (f) {
        hadError = true;
        //TODO: should this be "firebase_auth/user-not-found" now?
        // if (f.message == 'user-not-found') {
        // Delay
        loge(f);
        // } else {
        // rethrow;
        // }
      }

      if (hadError) {
        logd('AUTH SIGN IN FAILED. Waiting a second then trying again');
        await Future.delayed(const Duration(seconds: 1));
        continue;
      }
      // Got here so there's no error
      break;
    }

    if (i >= maxRetries) {
      //TODO: a better error?

      return left(AuthServiceSignInFailure.unexpected);
    }

    logd('Signed in with one time password');

    return right(unit);
  } on FirebaseFunctionsException catch (e) {
    logd('registerUserWithJustEmail 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('registerUserWithJustEmail other exception');
    loge(e);
    return left(AuthServiceSignInFailure.unexpected);
  }
}