Flutter Firebase Phone Authentication not working

4,940

Solution 1

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 .

Solution 2

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.

Share:
4,940
JIJO J
Author by

JIJO J

Updated on December 15, 2022

Comments

  • JIJO J
    JIJO J over 1 year

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