import { createContext, useContext, useEffect, useState } from "react"; import { auth } from "./backendAuth"; const AuthContext = createContext(null); /** * SupabaseAuth — provides auth state and helper methods to the tree. * * All auth is handled server-side via httpOnly cookies. * The frontend never sees raw tokens. */ export function AuthProvider({ children }) { const [user, setUser] = useState(null); // Restore session on mount (from httpOnly cookie) useEffect(() => { auth.me().then(({ user }) => { setUser(user); }); }, []); const signIn = async (email, password) => { const data = await auth.signin(email, password); setUser(data.user); return data; }; const signUp = async (email, password) => { const data = await auth.signup(email, password); setUser(data.user); return data; }; const signOut = async () => { await auth.signout(); setUser(null); }; const getToken = async () => { // Token lives in httpOnly cookie — not accessible to JavaScript return null; }; return ( {children} ); } export function useAuth() { const ctx = useContext(AuthContext); if (!ctx) throw new Error("useAuth must be used within AuthProvider"); return ctx; }