Last active
June 30, 2021 15:13
-
-
Save RacketyWater7/a9a0f527834e3b3b4b4ae48de053e928 to your computer and use it in GitHub Desktop.
This file contains 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 characters
import axios from "axios"; | |
import Cookies from "js-cookie"; | |
import TokenValidate from "./TokenValidate"; | |
//Request Interceptor | |
axios.interceptors.request.use( | |
async (config) => { | |
if (config.url.includes("/login")) return config; | |
if (config.url.includes("/refreshToken")) return config; | |
} | |
TokenValidate(); | |
config.headers["Authorization"] = "Bearer " + Cookies.get("access"); | |
config.headers["Content-Type"] = "application/json"; | |
return config; | |
}, | |
(error) => { | |
return Promise.reject(error); | |
} | |
); | |
// Response Interceptor | |
axios.interceptors.response.use( | |
(response) => { | |
return response; | |
}, | |
(error) => { | |
const request = error.config; //this is actual request that was sent, and error is received in response to that request | |
if (error.response.status === 401 && !request._retry) { | |
request._retry = true; | |
axios.defaults.headers.common["Authorization"] = | |
"Bearer " + Cookies.get("access"); | |
axios.defaults.headers.common["Content-Type"] = "application/json"; | |
return axios(request); | |
} | |
return Promise.reject(error); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment