title: Firebase Phone Authentication Error In Flutter - The sms code has expired. Please re-send the verification code to try again
body: <p>I implemented Firebase phone authentication (OTP Login) successfully and it was working fine. However, I had to <strong>change my keystore alias and generate a new keystore</strong> to update the app in Play Store.</p>
  <p>Since then, I am facing this issue where it says <strong>the otp is wrong every time</strong>. Even when I copy-paste the otp from message, it throws the error every time. It is also behaving weird, for some phone numbers, the otp works successfully but for some others, it doesn't. Any little help will be appreciated a lot.</p>
  <p><em><strong>It is important to note that OTP Authentication is working fine in emulators but the error shows up when installed in a real device.</strong></em></p>
  <p><strong>Here's my code snippet below -</strong></p>
  <pre><code>String verficationIdRecieved = &quot;&quot;;
    int? _resendToken;
    void verifyNumber() {
      auth.verifyPhoneNumber(
        phoneNumber: &quot;+880&quot; + phoneNoController.text,
        verificationCompleted: (PhoneAuthCredential credential) async {
          await auth.signInWithCredential(credential).then((value) async {
            print(&quot;verficationCompleted  :  Logged in&quot;);
            // Get.offAll(() =&gt; OtpVerifyCodeScreen);
          });
        },
        verificationFailed: (FirebaseAuthException exception) {
          LoadingDialog().dismiss();
          print(exception.message);
          getPrefix.Get.snackbar(&quot;Error verifyNumber&quot;,
              &quot;Please check if your phone number is right. ${exception.message}&quot;);
        },
        codeSent: (String verficationId, int? resendToken) async {
          verficationIdRecieved = verficationId;
          _resendToken = resendToken;
          otpCodeVisible = true;
          setState(() {
            timedButtonActtive = true;
          });
          LoadingDialog().dismiss();
        },
        timeout: Duration(seconds: 60),
        forceResendingToken: _resendToken,
        codeAutoRetrievalTimeout: (String verificationId) {
          // ADDED LATER https://stackoverflow.com/questions/61132218/resend-otp-code-firebase-phone-authentication-in-flutter
          // REMOVE IF ERROR
          verficationIdRecieved = verificationId;
        },
      );
    }
  
    var firebaseToken;
    void verifyCode(BuildContext context) async {
      LoadingDialog().show(context);
      PhoneAuthCredential credential = PhoneAuthProvider.credential(
          verificationId: verficationIdRecieved, smsCode: codeController.text);
      try {
        await auth.signInWithCredential(credential).then((value) {
          // print(&quot;\n\n User UID should be   ${value.user!.uid}&quot;);
          print(&quot;verifyCode  :  Login successfull&quot;);
          value.user!.getIdToken().then((value) =&gt; {
                firebaseToken = value,
                print(&quot;This is the firebase token  ==  $firebaseToken&quot;),
                verifyOtp(
                    mobileNo: phoneNoController.text,
                    otp: codeController.text,
                    ftoken: firebaseToken)
              });
          final snackBar = SnackBar(content: Text(&quot;Login Successful&quot;));
          ScaffoldMessenger.of(context).showSnackBar(snackBar);
          // getPrefix.Get.offAll(() =&gt; SetUsernameScreen());
        });
      } catch (e) {
        final snackBar = SnackBar(
            content: Text(
                &quot;The sms code has expired or you entered a wrong code. Please re-send the code to try again.&quot;));
        ScaffoldMessenger.of(context).showSnackBar(snackBar);
      }
    }
  </code></pre>