flutter_firebase_phone_auth_riverpod icon indicating copy to clipboard operation
flutter_firebase_phone_auth_riverpod copied to clipboard

Error thrown in verificationFailed does not propagate to calling method

Open tchouaffe opened this issue 8 months ago • 0 comments

I have this 2 methods in flutter for firebase phone authentication:

Future<void> verifyPhone(Function() completion) async {
    _logger.i("start verifyPhone");
    await _firebaseAuth.verifyPhoneNumber(
      phoneNumber: _phoneNumber?['e164'],
      verificationCompleted: (AuthCredential credential) async {
        _logger.i("verifyPhone completed");
        await _firebaseAuth.signInWithCredential(credential);
      },
      verificationFailed: (FirebaseException e) {
        _logger.e("verifyPhone failed", error: e.code);
        throw e;
      },
      codeSent: (String verificationId, int? resendToken) {
        _logger.i("verifyPhone codeSent");
        _verificationId = verificationId;
        completion();
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        _logger.i("verifyPhone codeAutoRetrievalTimeout");
        _verificationId = verificationId;
        completion();
      }
      timeout: const Duration(seconds: 120),
    );
  }

verifyPhone is then called by

Future<void> callVerifyPhone() async {
    state = const SignInState.loading();
    try {
      await ref.read(authServiceProvider.notifier).verifyPhone(() {
        state = const SignInState.success();
        _analytics.logEvent(
          name: 'verifyPhone',
          parameters: {
        "phone_number": ref.read(authServiceProvider).formattedPhoneNumber,
          },
        );
      });
    } catch (e) {
      FirebaseCrashlytics.instance.recordError(
        e,
        null,
        information: ['verifyPhone failed'],
      );
      _logger.e('verifyPhone failed: $e', error: e);
      state = const SignInState.error(SignInError.error);
    }
  }

the exception thrown within verificationFailed never reaches the callVerifyPhone catch block.

tchouaffe avatar Jun 04 '24 08:06 tchouaffe