mirror of
https://github.com/ramvignesh-b/pi-ku.git
synced 2026-05-04 15:56:56 +00:00
refactor: implement automatic token refresh and credential support for auth API client
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user