feat: refactor frontend routing and authentication
- Changed root div ID from 'root' to 'app' in index.html. - Updated package.json to include new dependencies for routing and state management. - Removed App component and replaced it with a router setup in main.tsx. - Added route definitions for login, about, and index pages. - Implemented authentication logic using Zustand for state management. - Created API client with Axios for handling requests and token management. - Added CORS support in the backend API. - Updated schema for login requests to use camelCase.
This commit is contained in:
33
libertas-frontend/src/services/api-client.ts
Normal file
33
libertas-frontend/src/services/api-client.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import axios from 'axios'
|
||||
import { useAuthStorage } from '@/hooks/use-auth-storage'
|
||||
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: 'http://localhost:8080/api/v1',
|
||||
})
|
||||
|
||||
apiClient.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = useAuthStorage.getState().token
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
useAuthStorage.getState().clearToken()
|
||||
window.location.reload()
|
||||
}
|
||||
return Promise.reject(error)
|
||||
},
|
||||
)
|
||||
|
||||
export default apiClient
|
||||
Reference in New Issue
Block a user