import { createContext, useContext, useState, useEffect, type ReactNode } from 'react'; import { apiFetch } from '../lib/api'; export interface User { id: string; email: string | null; nickname: string; subscriptionLevel?: number; } interface AuthContextValue { user: User | null; loading: boolean; setUser: (u: User | null) => void; } const AuthContext = createContext(null); interface MeResponse { success: boolean; data: { user: User }; } export function AuthProvider({ children }: { children: ReactNode }) { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { let cancelled = false; apiFetch('/auth/me') .then((res) => { if (!cancelled) setUser(res.data.user); }) .catch(() => { if (!cancelled) setUser(null); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, []); return ( {children} ); } export function useAuthContext(): AuthContextValue { const ctx = useContext(AuthContext); if (!ctx) throw new Error('useAuthContext must be used inside AuthProvider'); return ctx; }