25 lines
614 B
TypeScript
25 lines
614 B
TypeScript
import axios from "axios";
|
|
|
|
const instance = axios.create({
|
|
baseURL: import.meta.env.VITE_API_URL,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
});
|
|
|
|
// Request interceptor: автоматически подставляет JWT, если есть
|
|
instance.interceptors.request.use(
|
|
(config) => {
|
|
const token = localStorage.getItem("jwt"); // или можно брать из Redux через store.getState()
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default instance;
|