loginWithPhone method

  1. @override
Future<Either<PhoneAuthError, Unit>> loginWithPhone(
  1. String phoneNumber, {
  2. required Function verificationCompleted,
  3. required dynamic verificationFailed(
    1. PhoneAuthError
    ),
  4. required Function codeSent,
  5. required Function codeAutoRetrievalTimeout,
  6. required bool codeResend,
})
override

Implementation

@override
Future<Either<PhoneAuthError, Unit>> loginWithPhone(
  String phoneNumber, {
  required Function verificationCompleted,
  required Function(PhoneAuthError) verificationFailed,
  required Function codeSent,
  required Function codeAutoRetrievalTimeout,
  //TODO: this is unused right now because the code kind of handles either case
  required bool codeResend,
}) async {
  assert(phoneNumber.isNotEmpty);

  // Normalize the phone number

  if (_lastUsedPhoneNumberForLogin != phoneNumber) {
    logd('AuthService: Phone number changed, clearing resend token');
    _resendToken = null;
  }

  try {
    await _fbAuth.verifyPhoneNumber(
      phoneNumber: phoneNumber,
      forceResendingToken: _resendToken,
      verificationCompleted: (PhoneAuthCredential credential) async {
        // Only on Android
        // This callback will be here for instant verification and auto-retrieval. For those cases, we can sign in the user directly
        await _fbAuth.signInWithCredential(credential);
        verificationCompleted();
      },
      verificationFailed: (FirebaseAuthException fe) {
        // Handle failed verification
        loge(fe, 'verificationFaild');

        if (fe.code == 'invalid-phone-number') {
          verificationFailed(PhoneAuthError.invalidPhoneNumber);
        } else if (fe.code == 'user-disabled') {
          verificationFailed(PhoneAuthError.userDisabled);
        } else if (fe.code == 'captcha-check-failed') {
          verificationFailed(PhoneAuthError.captchaCheckFailed);
        } else if (fe.code == 'too-many-requests') {
          verificationFailed(PhoneAuthError.tooManyRequests);
        } else {
          verificationFailed(PhoneAuthError.unexpected);
        }
      },
      codeSent: (String verificationId, int? resendToken) {
        _lastUsedPhoneNumberForLogin = phoneNumber;
        _phoneVerificationId = verificationId;
        _resendToken = resendToken;
        codeSent();
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        // Auto-retrieval timeout
        codeAutoRetrievalTimeout();
      },
    );
  } on FirebaseException catch (fe) {
    loge(fe);
    if (fe.code == 'invalid-phone-number') {
      return left(PhoneAuthError.invalidPhoneNumber);
    } else if (fe.code == 'user-disabled') {
      return left(PhoneAuthError.userDisabled);
    } else if (fe.code == 'captcha-check-failed') {
      return left(PhoneAuthError.captchaCheckFailed);
    } else if (fe.code == 'too-many-requests') {
      return left(PhoneAuthError.tooManyRequests);
    } else {
      return left(PhoneAuthError.unexpected);
    }
  } catch (e) {
    loge(e);
    return left(PhoneAuthError.unexpected);
  }

  return right(unit);
}