Spaces:
Sleeping
Sleeping
| import axios from 'axios'; | |
| const api = axios.create({ | |
| baseURL: (window.location.hostname === '127.0.0.1' || window.location.port === '5173') | |
| ? 'http://localhost:3000/api' | |
| : '/api', | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| } | |
| }); | |
| // Intercept requests and attach the JWT token if available | |
| api.interceptors.request.use((config) => { | |
| const token = localStorage.getItem('admin_token'); | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| return config; | |
| }); | |
| // Intercept responses for auth errors globally | |
| api.interceptors.response.use( | |
| response => response, | |
| error => { | |
| if (error.response && error.response.status === 401) { | |
| localStorage.removeItem('admin_token'); | |
| if (window.location.pathname !== '/login') { | |
| window.location.href = '/login'; | |
| } | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export const authAPI = { | |
| login: async (password) => { | |
| const { data } = await api.post('/settings/admin-login', { password }); | |
| return data; | |
| } | |
| }; | |
| export const ordersAPI = { | |
| getAll: async () => { | |
| const { data } = await api.get('/orders'); | |
| return data; | |
| }, | |
| updateStatus: async (orderId, status) => { | |
| const { data } = await api.put(`/orders/${orderId}/status`, { status }); | |
| return data; | |
| } | |
| }; | |
| export const productsAPI = { | |
| getAll: async () => { | |
| const { data } = await api.get('/products'); | |
| return data; | |
| }, | |
| create: async (productData) => { | |
| const { data } = await api.post('/products', productData); | |
| return data; | |
| }, | |
| update: async (id, productData) => { | |
| const { data } = await api.put(`/products/${id}`, productData); | |
| return data; | |
| }, | |
| delete: async (id) => { | |
| const { data } = await api.delete(`/products/${id}`); | |
| return data; | |
| } | |
| }; | |
| export const settingsAPI = { | |
| get: async () => { | |
| const { data } = await api.get('/settings'); | |
| return data; | |
| }, | |
| update: async (settingsData) => { | |
| const { data } = await api.put('/settings', settingsData); | |
| return data; | |
| } | |
| }; | |
| export default api; | |