-1

i have react redux application my issue when the token expired , i facing issue how to refresh the token and recall the api, this code i call when i get any API in my app

First action I invoke it's getWithAuth() and in this way I'm handling refresh token i know I'm doing wrong.

(The code is not working actually just I'm showing example) https://stackblitz.com/edit/react-kdujea?file=apiManager.js

I want when I call getWithAuth or any api and I get result 401. I want refresh token and if I result 401 then logout.

halfer
  • 19,824
  • 17
  • 99
  • 186
Zaher Zaki
  • 131
  • 1
  • 2
  • 11

1 Answers1

0

You can use axios interceptors to handle unauthorized 401 issue in one place e.g.

axios.interceptors.response.use(function (response) {
return response;
}, function (error) {
  if(error.status === 401) {
    // do something when unauthorized
  }
  return Promise.reject(error);
});
Umair Farooq
  • 1,763
  • 13
  • 16