import { createContext, useContext, useState, useEffect, ReactNode } from 'react'; import api from '../lib/api'; interface User { id: number; username: string; } interface AuthContextType { user: User | null; login: (username: string, password: string) => Promise; register: (username: string, password: string) => Promise; logout: () => void; isLoading: boolean; } const AuthContext = createContext(undefined); export function AuthProvider({ children }: { children: ReactNode }) { const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); useEffect(() => { // Try to authenticate with existing cookie api .get('/auth/me') .then((res) => setUser(res.data)) .catch(() => setUser(null)) .finally(() => setIsLoading(false)); }, []); const login = async (username: string, password: string) => { const res = await api.post('/auth/login', { username, password }); setUser(res.data.user); }; const register = async (username: string, password: string) => { const res = await api.post('/auth/register', { username, password }); setUser(res.data.user); }; const logout = async () => { try { await api.post('/auth/logout'); } catch { // Ignore errors during logout } setUser(null); }; return ( {children} ); } export function useAuth() { const ctx = useContext(AuthContext); if (!ctx) throw new Error('useAuth must be used within AuthProvider'); return ctx; }