From 2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Tue, 25 Mar 2025 17:23:30 -0400 Subject: Implemented admin dashboard --- web/src/utils/api.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 web/src/utils/api.js (limited to 'web/src/utils') 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; +}; -- cgit v1.2.3