Last active
September 20, 2025 02:01
-
Star
(232)
You must be signed in to star a gist -
Fork
(42)
You must be signed in to fork a gist
-
-
Save mkjiau/650013a99c341c9f23ca00ccb213db1c to your computer and use it in GitHub Desktop.
Revisions
-
mkjiau revised this gist
Dec 27, 2017 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ let isRefreshing = false; let refreshSubscribers = []; -
mkjiau revised this gist
Dec 27, 2017 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ let instance; let isRefreshing = false; let refreshSubscribers = []; const instance = axios.create({ baseURL: Config.API_URL, -
mkjiau revised this gist
Dec 27, 2017 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,16 +11,14 @@ instance.interceptors.response.use(response => { }, error => { const { config, response: { status } } = error; const originalRequest = config; if (status === 498) { if (!isRefreshing) { isRefreshing = true; refreshAccessToken() .then(newToken => { isRefreshing = false; onRrefreshed(newToken); }); } -
mkjiau created this gist
Dec 27, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ static instance; static isRefreshing = false; static refreshSubscribers = []; const instance = axios.create({ baseURL: Config.API_URL, }); instance.interceptors.response.use(response => { return response; }, error => { const { config, response: { status } } = error; const originalRequest = config; const { dispatch, getState } = store; // Redux store if (status === 498) { if (!isRefreshing) { isRefreshing = true; const { token: expiredToken } = getState().auth; dispatch(refreshAccessToken(expiredToken)) .then(token => { isRefreshing = false; onRrefreshed(token); }); } const retryOrigReq = new Promise((resolve, reject) => { subscribeTokenRefresh(token => { // replace the expired token and retry originalRequest.headers['Authorization'] = 'Bearer ' + token; resolve(axios(originalRequest)); }); }); return retryOrigReq; } else { return Promise.reject(error); } }); subscribeTokenRefresh(cb) { refreshSubscribers.push(cb); } onRrefreshed(token) { refreshSubscribers.map(cb => cb(token)); }