Frontend update

This commit is contained in:
2026-06-26 21:51:27 -05:00
parent eef79e2403
commit 6dba6a017c
48 changed files with 5004 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
import { createContext, useContext, useEffect, useState, useCallback } from 'react'
import { api } from '../api/client.js'
const AuthContext = createContext(null)
export function AuthProvider({ children }) {
const [user, setUser] = useState(null)
const [loading, setLoading] = useState(true)
const refresh = useCallback(async () => {
try {
const data = await api.me()
setUser(data.user)
} catch {
setUser(null)
} finally {
setLoading(false)
}
}, [])
useEffect(() => {
refresh()
}, [refresh])
const login = useCallback(async (username, password) => {
const data = await api.login(username, password)
setUser(data.user)
return data.user
}, [])
const logout = useCallback(async () => {
try {
await api.logout()
} finally {
setUser(null)
}
}, [])
return (
<AuthContext.Provider value={{ user, loading, login, logout, refresh }}>
{children}
</AuthContext.Provider>
)
}
export function useAuth() {
const ctx = useContext(AuthContext)
if (!ctx) throw new Error('useAuth must be used within AuthProvider')
return ctx
}