Remove decorative icons and update CORS configuration
This commit is contained in:
86
src/services/api.ts
Normal file
86
src/services/api.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000/api';
|
||||
|
||||
// Create axios instance with default config
|
||||
const api = axios.create({
|
||||
baseURL: API_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Add auth token to requests
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('authToken');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
// Handle auth errors
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401 || error.response?.status === 403) {
|
||||
localStorage.removeItem('authToken');
|
||||
localStorage.removeItem('tokenExpiry');
|
||||
window.location.href = '/upadaj';
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export const authService = {
|
||||
login: async (username: string, password: string) => {
|
||||
const response = await api.post('/login', { username, password });
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
export const colorService = {
|
||||
getAll: async () => {
|
||||
const response = await api.get('/colors');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
create: async (color: { name: string; hex: string }) => {
|
||||
const response = await api.post('/colors', color);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
update: async (id: string, color: { name: string; hex: string }) => {
|
||||
const response = await api.put(`/colors/${id}`, color);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
delete: async (id: string) => {
|
||||
const response = await api.delete(`/colors/${id}`);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
export const filamentService = {
|
||||
getAll: async () => {
|
||||
const response = await api.get('/filaments');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
create: async (filament: any) => {
|
||||
const response = await api.post('/filaments', filament);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
update: async (id: string, filament: any) => {
|
||||
const response = await api.put(`/filaments/${id}`, filament);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
delete: async (id: string) => {
|
||||
const response = await api.delete(`/filaments/${id}`);
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user