Initial commit: AARS backend + frontend + holiday/leave phase 1+2

This commit is contained in:
IT Dog
2026-07-19 20:10:15 +08:00
commit bbc0048c24
49 changed files with 13375 additions and 0 deletions
+52
View File
@@ -0,0 +1,52 @@
import { createContext, useContext, useState, useEffect } from 'react'
import api from '../api'
const AuthContext = createContext(null)
export function AuthProvider({ children }) {
const [user, setUser] = useState(null)
const [loading, setLoading] = useState(true)
useEffect(() => {
const token = localStorage.getItem('aars_token')
if (token) {
api.defaults.headers.common['Authorization'] = `Bearer ${token}`
fetchUser()
} else {
setLoading(false)
}
}, [])
const fetchUser = async () => {
try {
const { data } = await api.get('/api/auth/me')
setUser(data)
} catch (err) {
localStorage.removeItem('aars_token')
delete api.defaults.headers.common['Authorization']
} finally {
setLoading(false)
}
}
const login = async (email, password) => {
const { data } = await api.post('/api/auth/login', { email, password })
localStorage.setItem('aars_token', data.access_token)
api.defaults.headers.common['Authorization'] = `Bearer ${data.access_token}`
await fetchUser()
}
const logout = () => {
localStorage.removeItem('aars_token')
delete api.defaults.headers.common['Authorization']
setUser(null)
}
return (
<AuthContext.Provider value={{ user, loading, login, logout }}>
{children}
</AuthContext.Provider>
)
}
export const useAuth = () => useContext(AuthContext)