title: Firebase phone auth Error: Invalid token. at nativeToJSError
body: <p>I am using <code>firebase</code> phone auth in react native app with this <a href="https://rnfirebase.io/" rel="noreferrer">docs</a>, I tested it in some different devices. sometimes the phone <code>auth</code> works and sometimes it throws this error</p>
  
  <blockquote>
    <p>firebase phone auth Error: Invalid token. at nativeToJSError</p>
  </blockquote>
  
  <p><img src="https://i.stack.imgur.com/InpP7.jpg" alt="enter image description here"></p>
  
  <p>I have gone through the docs of firebase and try to understand this error.
  Here is my code snippet:</p>
  
  <pre class="lang-js prettyprint-override"><code>confirmPhone = async(phoneNumber) =&gt; {
    const phoneWithAreaCode = phoneNumber.replace(/^0+/, '+972');
    return new Promise((res, rej) =&gt; {
      firebase.auth().verifyPhoneNumber(phoneWithAreaCode)
        .on('state_changed', async(phoneAuthSnapshot) =&gt; {
          switch (phoneAuthSnapshot.state) {
            case firebase.auth.PhoneAuthState.AUTO_VERIFIED:
              UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
              await this.confirmCode(phoneAuthSnapshot.verificationId, phoneAuthSnapshot.code, phoneAuthSnapshot)
              res(phoneAuthSnapshot)
  
              break
  
            case firebase.auth.PhoneAuthState.CODE_SENT:
              UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
              res(phoneAuthSnapshot)
              break
  
            case firebase.auth.PhoneAuthState.AUTO_VERIFY_TIMEOUT:
              UserStore.setVerificationId(phoneAuthSnapshot.verificationId)
              UserStore.setErrorCodeAuthentication('SMS code has expired')
              res(phoneAuthSnapshot)
  
  
            case firebase.auth.PhoneAuthState.ERROR:
              console.log(phoneAuthSnapshot.error.code)
              if (phoneAuthSnapshot.error) {
                this.showMessageErrorByCode(phoneAuthSnapshot.error.code)
  
              }
  
              rej(phoneAuthSnapshot)
              break
  
          }
        })
    })
  }
  
  confirmCode = async(verificationId, code, phoneAuthSnapshot) =&gt; {
    console.log(verificationId, code);
    try {
      const credential = await firebase.auth.PhoneAuthProvider.credential(verificationId, code)
      UserStore.setCodeInput(code)
      UserStore.setUserCredentials(credential)
      AppStore.setAlreadyVerifiedAuto(true)
      await this.authenticate(credential)
      return credential
    } catch (e) {
      console.log(e)
      this.showMessageErrorByCode(e.error.code)
  
  
      //    throw new Error(e)
    }
  }
  
  showMessageErrorByCode(errorcode) {
    switch (errorcode) {
      case 'auth/invalid-phone-number':
        UserStore.setErrorCodeAuthentication('Please enter valid phone number');
        break;
      case 'auth/unknown':
        {
          UserStore.setErrorCodeAuthentication('User blocked by firebase');
          break;
        }
  
      case 'auth/session-expired':
        {
          UserStore.setErrorCodeAuthentication('SMS code has expired');
          break;
        }
      case 'auth/invalid-verification-code':
        {
          UserStore.setErrorCodeAuthentication('Code verification not correct');
          break;
        }
      default:
        UserStore.setErrorCodeAuthentication('other error');
  
    }
  }
  
  authenticate = async(credential) =&gt; {
    await firebase.auth().signInAndRetrieveDataWithCredential(credential)
  
  }
  
  //...
  </code></pre>