2024-12-04 17:28:40 +00:00
|
|
|
import axios from "axios";
|
|
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
import { login } from "../login-services";
|
|
|
|
|
|
|
|
|
|
const baseURL = "https://netidhub.com/api/";
|
|
|
|
|
|
|
|
|
|
const refreshToken = Cookies.get("refresh_token");
|
|
|
|
|
|
|
|
|
|
const axiosInterceptorInstance = axios.create({
|
|
|
|
|
baseURL,
|
|
|
|
|
headers: {
|
|
|
|
|
"content-type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Request interceptor
|
|
|
|
|
axiosInterceptorInstance.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
console.log("Config interceptor : ", config);
|
|
|
|
|
const accessToken = Cookies.get("access_token");
|
|
|
|
|
if (accessToken) {
|
|
|
|
|
if (config.headers)
|
|
|
|
|
config.headers.Authorization = "Bearer " + accessToken;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Response interceptor
|
|
|
|
|
axiosInterceptorInstance.interceptors.response.use(
|
|
|
|
|
(response) => {
|
|
|
|
|
return response;
|
|
|
|
|
},
|
|
|
|
|
async function (error) {
|
|
|
|
|
const originalRequest = error.config;
|
|
|
|
|
if (error.response.status === 401 && !originalRequest._retry) {
|
|
|
|
|
originalRequest._retry = true;
|
|
|
|
|
const data = {
|
|
|
|
|
refreshToken: refreshToken,
|
2024-12-06 18:37:45 +00:00
|
|
|
grant_type: "refresh_token",
|
|
|
|
|
client_id: "mediahub-app",
|
2024-12-04 17:28:40 +00:00
|
|
|
};
|
|
|
|
|
const res = await login(data);
|
|
|
|
|
if (res.data?.data?.access_token) {
|
|
|
|
|
Cookies.set("access_token", res.data.data.access_token);
|
|
|
|
|
Cookies.set("refresh_token", res.data.data.refresh_token);
|
|
|
|
|
return axiosInterceptorInstance(originalRequest);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export default axiosInterceptorInstance;
|