diff options
| author | Andrew Lee <andrew@alee14.me> | 2025-03-25 17:23:30 -0400 |
|---|---|---|
| committer | Andrew Lee <andrew@alee14.me> | 2025-03-25 17:23:30 -0400 |
| commit | 2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80 (patch) | |
| tree | e02587b4dca4bd7027b4cc012d458b18392643ea /web/src/utils/api.js | |
| parent | 1c12d378d66b92b1674acd17640f2bac752da289 (diff) | |
| download | AleeBot-2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80.tar.gz AleeBot-2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80.tar.bz2 AleeBot-2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80.zip | |
Implemented admin dashboard
Diffstat (limited to 'web/src/utils/api.js')
| -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; +}; |
