Home > Software design >  Flutter - OTP Verification works in debug mode but not release mode iOS
Flutter - OTP Verification works in debug mode but not release mode iOS

Time:01-11

I'm building a flutter app and have encountered a weird problem that I haven't seen before. I'm using Firebase auth for phone verification through OTP.

However, I only receive OTPs on the debug mode for iOS and not the release mode. It works fine on Android in both cases. I'm not sure what I'm doing wrong as I have the updated GoogleService-info.plist in my iOS root. Do you guys have any ideas on how I can make it work?

This is my workflow when I verify the OTP:-

Future<void> verifyPhone(String phoneNumber, ) async {
    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: phoneNumber,
        timeout: const Duration(seconds: 60),
        verificationCompleted: (PhoneAuthCredential credential) {
          print('Auth completed');
        },
        verificationFailed: (FirebaseAuthException e) {
          print(e);
          print('Auth failed');
        },
        codeSent: (String verificationId, int? resendToken) {
          print('OTP sent');
          verId = verificationId;
          Queries.instance.verId = verificationId;
          print(verId   '........');
        },
        codeAutoRetrievalTimeout: (String verificationId) {
          print('Timeout');
        }
    );
  }

Any help and advice would be much appreciated! Thanks in advance :)

CodePudding user response:

For some Firebase related functions to work in iOS, you will have to link your Firebase project with Apple Push Notification Service's APN key.

Things you can do,

  1. Go to Apple Developer Console and create an APN key.
  2. Export the key and add it to Firebase Console.

For reference,

https://www.kodeco.com/20201639-firebase-cloud-messaging-for-ios-push-notifications

CodePudding user response:

Below-mentioned is my function for OTP verification:

verifyPhoneNumber() async {
    await FirebaseAuth.instance.verifyPhoneNumber(
      phoneNumber: widget.phoneNumberControllerValueText,
      verificationCompleted: (PhoneAuthCredential credential) async {
        UserCredential userCredential =
            await FirebaseAuth.instance.signInWithCredential(credential);
        if (userCredential.user != null) {
          log('verifyPhoneNumber - User logged in.');

          if (mounted) {
            Navigator.pushAndRemoveUntil(
              context,
              MaterialPageRoute(
                builder: (context) {
                  return const HomeScreen();
                },
              ),
              (Route<dynamic> route) => false,
            );
          }
        }
      },
      verificationFailed: (FirebaseAuthException e) {
        log('e.toString: ${e.message.toString()}');
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(
              e.message.toString(),
            ),
          ),
        );
        if (e.code == 'invalid-phone-number') {
          log('The provided phone number is not valid.');
          ScaffoldMessenger.of(context).showSnackBar(
            const SnackBar(
              content: Text(
                'The provided phone number is not valid.',
              ),
            ),
          );
        }
      },
      codeSent: (String verificationId, int? resendToken) async {
        tempVerificationId = verificationId;
      },
      codeAutoRetrievalTimeout: (String verificationId) {
        tempVerificationId = verificationId;
      },
      timeout: const Duration(seconds: 60),
    );
  }

You can cross-verify with your function to check the difference, or else if you want to full source code, it is available at:

https://github.com/dharambudh1/firebase-otp-integration-demo/

  • Related