import { createContext, useContext, useEffect, useState, type ReactNode } from "react"; import { api, getToken, setToken } from "./api"; import type { User } from "./types"; interface AuthState { user: User | null; loading: boolean; login: (email: string, password: string) => Promise; register: (b: { name: string; email: string; password: string; zip: string; phone?: string }) => Promise; logout: () => void; } const AuthContext = createContext(null); export function AuthProvider({ children }: { children: ReactNode }) { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { if (!getToken()) { setLoading(false); return; } api .me() .then(setUser) .catch(() => setToken(null)) .finally(() => setLoading(false)); }, []); async function login(email: string, password: string): Promise { const { access_token } = await api.login({ email, password }); setToken(access_token); const me = await api.me(); setUser(me); return me; } async function register(b: { name: string; email: string; password: string; zip: string; phone?: string }) { const { access_token } = await api.register(b); setToken(access_token); setUser(await api.me()); } function logout() { setToken(null); setUser(null); } return ( {children} ); } export function useAuth(): AuthState { const ctx = useContext(AuthContext); if (!ctx) throw new Error("useAuth must be used within AuthProvider"); return ctx; }