diff --git a/.env.local.example b/.env.local.example index 32c64a0..63aa21a 100644 --- a/.env.local.example +++ b/.env.local.example @@ -3,3 +3,6 @@ # Neshan (نشان) map API key — get one from https://platform.neshan.org # Used by components/MapSection.jsx for the static map image. NEXT_PUBLIC_NESHAN_API_KEY= + + +NEXT_PUBLIC_API_BASE_URL=https://your-real-backend.com \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e26be01..737dc5b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,14 +2,10 @@ + + - - - - - - diff --git a/app/page.jsx b/app/page.jsx index 5cd37db..5bfc5a8 100644 --- a/app/page.jsx +++ b/app/page.jsx @@ -18,7 +18,9 @@ export default function HomePage() { return (
+ + diff --git a/lib/axios.js b/lib/axios.js new file mode 100644 index 0000000..32f7867 --- /dev/null +++ b/lib/axios.js @@ -0,0 +1,59 @@ +import axios from 'axios'; + + +const api = axios.create({ + baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || 'https://api.example.com', + timeout: 15000, + headers: { + 'Content-Type': 'application/json', + }, +}); + + +api.interceptors.request.use( + (config) => { + const token = + typeof window !== 'undefined' ? window.localStorage.getItem('rm-auth-token') : null; + + if (token) { + config.headers.Authorization = `Bearer ${token}`; + } + +// console.log('[API request]', config.method?.toUpperCase(), config.url); + + return config; + + + }, + (error) => Promise.reject(error) +); + + +api.interceptors.response.use( + (response) => response, + + (error) => { + if (error.response) { + + const status = error.response.status; + + if (status === 401) { + // window.localStorage.removeItem('rm-auth-token'); + // window.location.href = '/login'; + } + + if (status >= 500) { + // message.error('خطایی در سرور رخ داد. لطفاً بعداً تلاش کنید.'); + } + } else if (error.request) { + // درخواست ارسال شد ولی هیچ جوابی نیومد (قطعی اینترنت، سرور خاموش و ...) + // TODO: message.error('اتصال با سرور برقرار نشد.'); + } + + + return Promise.reject(error); + + } +); + +export default api; \ No newline at end of file