46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
import axios from "axios";
|
|
import Cookies from "js-cookie";
|
|
import "react-toastify/dist/ReactToastify.css";
|
|
|
|
const BASE_URL = process.env.NEXT_PUBLIC_API_URL;
|
|
|
|
const api = axios.create({
|
|
baseURL: BASE_URL,
|
|
"X-CSRF-Token": "",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
// اضافه کردن interceptor برای افزودن token به ریکوستهایی که نیاز به احراز هویت دارند
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
// فقط اگر requireAuth در config تنظیم شده باشد، token اضافه میشود
|
|
if (config.requireAuth) {
|
|
const token = Cookies.get("access_token");
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
}
|
|
return config;
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
// if (error.status === 401) {
|
|
// removeToken();
|
|
// window.location.pathname = "login";
|
|
// }
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default api;
|