title: How to Create Middleware for refresh token in Reactjs with axios and redux
body: <p>i am working with reactjs on front end the issue is after certain time period the accessToken is expired and server send status of 401(unauthorized) then i need to send refresh token back to server it works fine until i manually send the refresh token i set the setInterval function but thats not a good approach how to automatically send it when token is expired.
  i also google it but everyone is talking about creating middleware anyone please give me the hint how to create that middleware or any other solution or link any article related to it . i created this but this didnt works for me however when server send status of 401 then middleware ran but it dosent dispatch my refreshToken() function</p>
  
  <pre><code>const customMiddleWare = store =&gt; next =&gt; action =&gt; {
    axios.interceptors.response.use(function (response) {
      return response;
    }, function (error) {
      if(error.status === 401) {
        // do something when unauthorized
        store.dispatch(refreshToken());
      }
      return Promise.reject(error);
    });
    console.log("Middleware triggered:", action);
    next(action);
  }
  </code></pre>
  
  <p>By the way i am using redux, redux-thunk and axios. thanks,</p>