skip to Main Content

My dart code isn’t catching the FirebaseAuthMultiFactorException required for logging in users that have opted in to MFA. For some reason, the exception is caught as a standard FirebaseAuthException.

Future handleLogin(String email, String pass, BuildContext context) async {
    try {
      await signInWithEmail(email, pass);
    } on FirebaseAuthMultiFactorException catch (e) {
      final firstHint = e.resolver.hints.first;
      if (firstHint is! PhoneMultiFactorInfo) {
        return;
      }
      await FirebaseAuth.instance.verifyPhoneNumber(
        multiFactorSession: e.resolver.session,
        multiFactorInfo: firstHint,
        verificationCompleted: (_) {},
        verificationFailed: (_) {},
        codeSent: (String verificationId, int? resendToken) async {
          final smsCode = await Navigator.push<String>(
            context,
            CupertinoPageRoute(builder: (context) => SmsCodePage()),
          );

          if (smsCode != null) {
            final credential = PhoneAuthProvider.credential(
              verificationId: verificationId,
              smsCode: smsCode,
            );

            try {
              await e.resolver.resolveSignIn(
                PhoneMultiFactorGenerator.getAssertion(
                  credential,
                ),
              );
            } on FirebaseAuthException catch (e) {
              print(e.message);
            }
          }
        },
        codeAutoRetrievalTimeout: (_) {},
      );
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-disabled') {
        return "banned";
      } else {
        print("firebase auth exception $e");
        return null;
      }
    } catch (e) {
      print("generic error: $e");
      return null;
    }
  }

Console output when attempting login with a user that has opted in to MFA (already setup):

flutter: firebase auth exception [firebase_auth/multi-factor-auth-required] 0x283bfcc90>}

Why is this being caught as a "standard" auth exception and not as a FirebaseAuthMultiFactorException?

2

Answers


  1. Turns out this was a bug in the FirebaseAuth plugin and was fixed in 4.7.2

    FIX(auth): fix MFA issue where the error wouldn't be properly catched (#11370). (72fef03f)
    
    Login or Signup to reply.
  2. ik , i even downgraded back firebase_auth from 4.9 to firebase_auth^4.7.2 . but it doesnt work. WHats wrong , i getting so annoyed because ive been stuck at this since yesterday . I checked firebase console and Cloud identity platform to enabled MFA, but everything checks out n is enabled. i cant figure out y it isnt working

    FirebaseAuthMultiFactorException catch (e) {
      final firstHint = e.resolver.hints.first;
      if (firstHint is! PhoneMultiFactorInfo) {
        return;
      }
    

    Im thinking of trying out aws amplify instead, but lets hope someone comes out wit a solution soon.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search