refactor: implement automatic token refresh and credential support for auth API client

This commit is contained in:
Your Name
2026-04-10 18:34:28 +05:30
parent 867839f896
commit a0eec02ee4
4 changed files with 27 additions and 7 deletions
+22 -2
View File
@@ -1,10 +1,30 @@
import axios from "axios";
const apiClient = axios.create({
const authApiClient = axios.create({
baseURL: `${import.meta.env.VITE_API_URL}/api/auth/`,
withCredentials: true,
headers: {
"Content-Type": "application/json",
},
});
export default apiClient;
authApiClient.interceptors.response.use(
(response) => response,
async (error) => {
if (error.response.status === 401) {
// token expired, refresh it
try {
const response = await authApiClient.post("refresh/");
if (response.status === 200) {
// refresh successful, retry the request
return authApiClient(error.config);
}
} catch (error) {
return Promise.reject(error);
}
}
return Promise.reject(error);
},
);
export default authApiClient;