4

Phone authentication getting failed with the following exception :

PlatformException(ERROR_SESSION_EXPIRED, The sms code has expired. Please re-send the verification code to try again., null)

But it works if I use a different phone number other than that on my phone. I've added both SHA-1 and SHA-256 fingerprints from the play store to firebase and also replaced the google-services.json.

Here's my code :

 void _verifyPhoneNumber() async {
    setState(() {
       isVerified=true; 
      });
    setState(() {
      _message = '';
    });
    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential phoneAuthCredential) {
      _auth.signInWithCredential(phoneAuthCredential);
      setState(() {
        _message = 'Received phone auth credential: $phoneAuthCredential';

      });
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
        _message =
            'Phone number verification failed';

    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      _verificationId = verificationId;

    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      _verificationId = verificationId;
    };

    await _auth.verifyPhoneNumber(
        phoneNumber: '+91'+_phoneNumberController.text,
        timeout: const Duration(seconds: 5),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

  // Example code of how to sign in with phone.
  void _signInWithPhoneNumber() async {
    setState(() {
      isLoading=true;
    });
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: _verificationId,
      smsCode: _smsController.text,
    );
    try{
      firebaseUser =
        (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(firebaseUser.uid == currentUser.uid);
      if (firebaseUser != null) {
.....

      } else {

        _message = 'Sign in failed';
        showErrorDialog();
      }

    }catch (e){
      showErrorDialog();
    }
    setState(() {
      isLoading=false;
    });
  }
1

4 Answers 4

3

not sure of your problem but it says : ERROR_SESSION_EXPIRED, The sms code has expired and in _auth.verifyPhoneNumber() your Timeout duration is quite low. try 60 Seconds.

await _auth.verifyPhoneNumber(
        phoneNumber: '+91${_phoneNumberController.text}',
        timeout: Duration(seconds: 60),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);

and if this didn't help give a look at the docs .

Sign up to request clarification or add additional context in comments.

4 Comments

Weird part is that the error occurs only if the sim is in same device
Sorry to hear that, you can check this post
Not true because the doc says: If you specified a positive value less than 30 seconds, library will default to 30 seconds.
The timeout is not for SMS validity. Please read the documentation. It's auto verify timeout. Doc: "The maximum amount of time you are willing to wait for SMS auto-retrieval to be completed by the library. Maximum allowed value is 2 minutes"
1

Its late ,but for others who are struggling, it's all is about the incomplete explanation of docs for login and signup.

comment the code of signin in _verifyPhoneNumber() function, add async to the function and make AuthCredential to PhoneAuthCredential

  final PhoneVerificationCompleted verificationCompleted =
    (PhoneAuthCredential phoneAuthCredential) async{
 // comment the below code for _auth.signIn and add async

 // _auth.signInWithCredential(phoneAuthCredential);
  setState(() {
    _message = 'Received phone auth credential: $phoneAuthCredential';

  });

Actually the _verifyPhoneNumber() checks the number in database ,so you can redirect user direct to Home Screen from this function.As the signIn runs two times ,one here and one in signinwithPhone Number() , so it gives timeout error.

1 Comment

This works perfectly!
0

Right,although timeout property is 60s. But after receiving the verification code and pasting the code immediately, ERROR_SESSION_EXPIRED like you. note: only on Android,just receive on a few phone number.

Comments

0

If you missed timeout: Duration(seconds: 60) in your

_auth.verifyPhoneNumber(
        phoneNumber: '+91${_phoneNumberController.text}',
        timeout: Duration(seconds: 60),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);

This issue may happen

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.