27 lines
638 B
TypeScript
27 lines
638 B
TypeScript
import { deleteAuthToken, getAuthToken } from "@/utils/token-storage";
|
|
import axios from "axios";
|
|
|
|
|
|
const axiosInstance = axios.create({
|
|
baseURL: process.env.EXPO_PUBLIC_BACKEND_BASE_URL,
|
|
timeout: 30 * 1000
|
|
});
|
|
|
|
axiosInstance.interceptors.request.use(
|
|
async config => {
|
|
const token = await getAuthToken();
|
|
if (token) config.headers.Authorization = `Bearer ${token}`;
|
|
|
|
return config;
|
|
},
|
|
(error) => Promise.reject(error),
|
|
);
|
|
axiosInstance.interceptors.response.use(undefined, async (error) => {
|
|
if (error.response?.status === 401) {
|
|
await deleteAuthToken();
|
|
}
|
|
|
|
throw error;
|
|
});
|
|
|
|
export default axiosInstance; |