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

    final packageInfo = await AppConfigBase.getPackageInfo();
    var result = await authCallable.call({
      'action': 'registerWithJustEmail',
      'email': email,
      'isForApp': true, // deprecated: use bundleId
      'bundleId': packageInfo.packageName,
    });

    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++) {
      try {
        await _fbAuth.signInWithEmailAndPassword(
          email: email,
          password: result.data['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);
    }

    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);
  }
}