From 00bf1187813b475d6b9b5c47be90e5a5ae3884a6 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Tue, 27 Jan 2026 22:21:08 +1100 Subject: [PATCH] Add axios API client with auth interceptors --- frontend/src/api/axios.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 frontend/src/api/axios.ts diff --git a/frontend/src/api/axios.ts b/frontend/src/api/axios.ts new file mode 100644 index 0000000..3e45a1d --- /dev/null +++ b/frontend/src/api/axios.ts @@ -0,0 +1,34 @@ +import axios from 'axios'; + +const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8001'; + +const api = axios.create({ + baseURL: API_BASE_URL, + headers: { + 'Content-Type': 'application/json', + }, +}); + +// Add token to requests if it exists +api.interceptors.request.use((config) => { + const token = localStorage.getItem('token'); + if (token) { + config.headers.Authorization = `Bearer ${token}`; + } + return config; +}); + +// Handle 401 responses +api.interceptors.response.use( + (response) => response, + (error) => { + if (error.response?.status === 401) { + localStorage.removeItem('token'); + localStorage.removeItem('user'); + window.location.href = '/login'; + } + return Promise.reject(error); + } +); + +export default api;