title: Acquiring a new token with `axios.interceptors`
body: <p>When the token expires, I want to get a new token based on refresh_token. I have read that this can be obtained with <code>axios.interceptors</code>.</p>
  
  <p>Please check if: </p>
  
  <ul>
  <li>Have I correctly configured axios.interceptors?</li>
  <li>Have I placed it in the right place, i.e. above the<code>Items</code> class.</li>
  <li><code>axios.interceptors.response</code> is assigned to the<code>interceptor</code> variable. What should I do with this variable?</li>
  </ul>
  
  <p>In addition to `axios.interceptors', I need to get a new token. The token is valid for 24 hours.</p>
  
  <ul>
  <li>Do I have to wait 24 hours to test whether it works, or is it possible in a different way, faster?</li>
  <li>Where should I put 'client_id', 'secret_id', 'grant_type'?</li>
  </ul>
  
  <p>Code here: <a href="https://stackblitz.com/edit/react-pkea41" rel="noreferrer">https://stackblitz.com/edit/react-pkea41</a></p>
  
  <pre><code>import axios from 'axios';
  
  axios.defaults.baseURL = localStorage.getItem('domain');
  
  const interceptor = axios.interceptors.response.use(
    response =&gt; response,
    error =&gt; {
        // Reject promise if usual error
        if (errorResponse.status !== 401) {
            return Promise.reject(error);
        }
  
        /* 
         * When response code is 401, try to refresh the token.
         * Eject the interceptor so it doesn't loop in case
         * token refresh causes the 401 response
         */
        axios.interceptors.response.eject(interceptor);
  
        return axios.post('/api/refresh_token', {
            'refresh_token': JSON.parse(localStorage.getItem('token'))['refresh_token']
        }).then(response =&gt; {
            /*saveToken();*/
            localStorage.setItem('token', JSON.stringify(response.data));
            error.response.config.headers['Authorization'] = 'Bearer ' + response.data.access_token;
            return axios(error.response.config);
        }).catch(error =&gt; {
            /*destroyToken();*/
            localStorage.setItem('token', '');
            this.router.push('/login');
            return Promise.reject(error);
        }).finally(createAxiosResponseInterceptor);
    }
  );
  
  class Items extends Component {
  
    constructor (props) {
      super(props);
      this.state = {
  
      }
    }
  
  
    render () {
      return (
        &lt;div &gt;
  
        &lt;/div&gt;
      )
    }
  }
  
  
  render(&lt;Items /&gt;, document.getElementById('root'));
  </code></pre>