JIJO J
JIJO J

Reputation: 219

Flutter Firebase Phone Authentication not working

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;
    });
  }

Upvotes: 4

Views: 6212

Answers (4)

MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P

Reputation: 1259

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

Upvotes: 0

Sarthakpandit
Sarthakpandit

Reputation: 111

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.

Upvotes: 1

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.

Upvotes: 0

Abdelbaki Boukerche
Abdelbaki Boukerche

Reputation: 1692

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 .

Upvotes: 3

Related Questions