import { createContext, useContext, useMemo, useState, type ReactNode } from 'react' import { getToken, login as apiLogin, setToken as persistToken } from '../api/client' interface AuthContextValue { isAuthenticated: boolean login: (username: string, password: string) => Promise logout: () => void } const AuthContext = createContext(null) export function AuthProvider({ children }: { children: ReactNode }) { const [token, setTokenState] = useState(() => getToken()) const value = useMemo( () => ({ isAuthenticated: !!token, login: async (username: string, password: string) => { const newToken = await apiLogin(username, password) persistToken(newToken) setTokenState(newToken) }, logout: () => { persistToken(null) setTokenState(null) }, }), [token], ) return {children} } export function useAuth(): AuthContextValue { const ctx = useContext(AuthContext) if (!ctx) throw new Error('useAuth must be used within AuthProvider') return ctx }