Skip to content

Instantly share code, notes, and snippets.

@RacketyWater7
Last active June 30, 2021 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RacketyWater7/a9a0f527834e3b3b4b4ae48de053e928 to your computer and use it in GitHub Desktop.
Save RacketyWater7/a9a0f527834e3b3b4b4ae48de053e928 to your computer and use it in GitHub Desktop.
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