feat: frontend auth flow (login, user management, audit logs, route guards)

This commit is contained in:
enzo
2026-07-22 16:55:11 +08:00
parent e13be8eb1c
commit 10404be62d
11 changed files with 672 additions and 70 deletions
+4
View File
@@ -0,0 +1,4 @@
import client from './client'
export const getAuditLogs = (params?: { page?: number; page_size?: number; action?: string }) =>
client.get('/audit', { params })
+12
View File
@@ -0,0 +1,12 @@
import client from './client'
export const login = (username: string, password: string) =>
client.post('/auth/login', { username, password })
export const register = (data: { username: string; email: string; password: string }) =>
client.post('/auth/register', data)
export const refresh = () => client.post('/auth/refresh')
export const changePassword = (data: { old_password: string; new_password: string }) =>
client.post('/auth/change-password', data)
+27
View File
@@ -0,0 +1,27 @@
import axios from 'axios'
const client = axios.create({
baseURL: '/api/v1',
headers: { 'Content-Type': 'application/json' },
})
client.interceptors.request.use((config) => {
const token = localStorage.getItem('access_token')
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
})
client.interceptors.response.use(
(res) => res,
(err) => {
if (err.response?.status === 401) {
localStorage.removeItem('access_token')
window.location.href = '/login'
}
return Promise.reject(err)
},
)
export default client
+11
View File
@@ -0,0 +1,11 @@
import client from './client'
export const getUsers = (params?: { page?: number; page_size?: number }) =>
client.get('/users', { params })
export const getUser = (id: string) => client.get(`/users/${id}`)
export const updateUser = (id: string, data: { role?: string; is_active?: boolean }) =>
client.patch(`/users/${id}`, data)
export const approveUser = (id: string) => client.post(`/users/${id}/approve`)