41 lines
944 B
TypeScript
41 lines
944 B
TypeScript
'use client'
|
|
|
|
import axios from 'axios'
|
|
import { error } from 'console';
|
|
import { cookies, headers } from 'next/headers'
|
|
|
|
const axiosInstance = axios.create({
|
|
baseURL: 'http://localhost:3000/api/',
|
|
timeout: 2000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
}
|
|
});
|
|
|
|
axiosInstance.interceptors.response.use((response)=> {
|
|
return response
|
|
}, async (error) =>{
|
|
|
|
const originalRequest = error.config;
|
|
|
|
if (error.response.status === 401 && !originalRequest._retry) {
|
|
|
|
originalRequest._retry = true; // Prevent infinite loops
|
|
|
|
try {
|
|
|
|
const res = await fetch("http://localhost:3000/api/auth/token/refresh/",{
|
|
method: "GET",
|
|
cache: "no-store",
|
|
});
|
|
console.log(res);
|
|
|
|
return axiosInstance(originalRequest)
|
|
|
|
} catch(refreshError) {
|
|
throw refreshError;
|
|
}
|
|
}
|
|
});
|
|
|
|
export default axiosInstance |