skip to Main Content

I am trying to implement OTP verification in my Flutter app using Firebase, but I am facing an issue where the verification fails even though both the sent and entered OTP are the same.

I am using the following function to verify the OTP:

void verifyOtp({
    required BuildContext context,
    required String verificationId,
    required String userOtp,
    required Function onSuccess,
  }) async {
    _isLoading = true;
    notifyListeners();
    try {
      PhoneAuthCredential creds = PhoneAuthProvider.credential(
          verificationId: verificationId, smsCode: userOtp);
      User? user = (await _firebaseAuth.signInWithCredential(creds)).user!;
      print('signwithcredential passed');
      if (user != null) {
        _uid = user.uid;
        onSuccess();
      }
    } on FirebaseAuthException catch (e) {
      print('failed Userotp: $userOtp');
      showSnackBar(context, e.message.toString());
      _isLoading = false;
      notifyListeners();
    }
  }

The error I am getting is "FirebaseAuthException: sms code has expired please resend the verification code to verify again."

I am new to Flutter and Firebase, so any help in fixing this problem would be greatly appreciated.

2

Answers


  1. Here are couple of things I have found maybe they will work:

    • Check the Firebase Console to ensure that the phone number being verified is correctly set up and that the SMS code is being sent to the right number.

    • Make sure that the phone number is being passed in the correct format.

    • FirebaseAuthException is being caught correctly. Make sure the message inside the catch block is being logged or displayed to the user.

    • Ensure that the dependencies and packages are correctly installed and imported.

    Login or Signup to reply.
  2. The solution is itself an error you are getting. You have to enter the code before it expire. Try to verify the otp as soon as you get the code as it’s saying "I/flutter ( 9598): error on firebaseAuthexception: [firebase_auth/session-expired] The sms code has expired. Please re-send the verification code to try again. ".

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