diff options
Diffstat (limited to 'web/src/utils')
| -rw-r--r-- | web/src/utils/api.js | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/web/src/utils/api.js b/web/src/utils/api.js new file mode 100644 index 0000000..4ba0e0c --- /dev/null +++ b/web/src/utils/api.js @@ -0,0 +1,29 @@ +// Utility for making authenticated API requests +export const fetchWithAuth = async (endpoint, options = {}) => { + const token = localStorage.getItem('token'); + const apiUrl = localStorage.getItem('apiUrl'); + + if (!token || !apiUrl) { + throw new Error('Not authenticated'); + } + + const headers = { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}`, + ...options.headers + }; + + const response = await fetch(`${apiUrl}${endpoint}`, { + ...options, + headers + }); + + // Handle token expiration + if (response.status === 401) { + localStorage.removeItem('token'); + window.location.href = '/'; + throw new Error('Session expired'); + } + + return response; +}; |
