title: How can i send firebase getIdToken with axios in reactjs?
body: <p>I am using the following code to obtain a user idtoken and i can succesfully log it to the console using console.log:</p>
  <pre><code>firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
          user.getIdToken().then(function(idToken) {  
             console.log(&quot;IDTOKEN:&quot;,idToken); 
  
            });
      }
  });
  </code></pre>
  <p>I am now trying to send the token as a header in a request using axios like this:</p>
  <pre><code>async updatebalance() {
    let id = this.props.id;
    if (id === null || id === undefined) {
      id = &quot;test&quot;;
    }
    var res = await axios.post(backUrl + &quot;account/user_load_balance&quot;, {
      uid: localStorage.getItem(&quot;account-info&quot;),
      id: id,
      idToken
    });
    if (res.data !== null &amp;&amp; res.data.status &amp;&amp; res.data.balance !== null &amp;&amp; res.data.balance !== []) {
      this.props.dispatch(etherBalanceLoaded(res.data.balance[0]));
      this.props.dispatch(tokenBalanceLoaded(res.data.balance[1]));
    }
    this.setState({
      showbuttonFlag: true
    });
    this.reset_values();
  }
  </code></pre>
  <p>however i get the following error:</p>
  <blockquote>
  <p>'idToken' is not defined</p>
  </blockquote>
  <p>I think i may need to set this as a global variable so it can be used in different function but i don't know how. How can i achieve this?</p>
  <p>Solution 1:</p>
  <pre><code>async updatebalance() {
          let id = this.props.id;
          if (id === null || id === undefined) {
              id = &quot;test&quot;;
          }
  const user = firebase.auth().currentUser
  if (user) {
    const idToken = await user.getIdToken();
    var res = await axios.post(backUrl + &quot;account/user_load_balance&quot;, {
      uid: localStorage.getItem(&quot;account-info&quot;),
      id: id,
      idToken
    });
  
  } else {
    console.log(&quot;User not logged in&quot;)
  }
          if (res.data !== null &amp;&amp; res.data.status &amp;&amp; res.data.balance !== null &amp;&amp; res.data.balance !== []) {
              this.props.dispatch(etherBalanceLoaded(res.data.balance[0]));
              this.props.dispatch(tokenBalanceLoaded(res.data.balance[1]));
          }
          this.setState({ showbuttonFlag: true });
          this.reset_values();
      }
  
      reset_values() {
          this.setState({ ethAmount: &quot;&quot; });
          this.setState({ sellethAmount: &quot;&quot; });
          this.setState({ ethPrice: &quot;&quot; });
          this.setState({ sellethPrice: &quot;&quot; });
          this.setState({ methAmount: &quot;&quot; });
          this.setState({ methPrice: &quot;&quot; });
          this.setState({ msellethAmount: &quot;&quot; });
          this.setState({ msellethPrice: &quot;&quot; });
      }
  </code></pre>