diff --git a/client/contexts/AuthContext.tsx b/client/contexts/AuthContext.tsx index 683254d0..fada6c2c 100644 --- a/client/contexts/AuthContext.tsx +++ b/client/contexts/AuthContext.tsx @@ -30,11 +30,43 @@ interface AuthContextType { } const AuthContext = createContext(undefined); +let warnedMissingProvider = false; + +const missingProviderFallback: AuthContextType = { + user: null, + profile: null, + roles: [], + session: null, + loading: true, + profileComplete: false, + signIn: async () => { + throw new Error("AuthProvider is not mounted. Please ensure your app is wrapped with ."); + }, + signUp: async () => { + throw new Error("AuthProvider is not mounted. Please ensure your app is wrapped with ."); + }, + signInWithOAuth: async () => { + throw new Error("AuthProvider is not mounted. Please ensure your app is wrapped with ."); + }, + signOut: async () => { + throw new Error("AuthProvider is not mounted. Please ensure your app is wrapped with ."); + }, + updateProfile: async () => { + throw new Error("AuthProvider is not mounted. Please ensure your app is wrapped with ."); + }, + refreshProfile: async () => { + throw new Error("AuthProvider is not mounted. Please ensure your app is wrapped with ."); + }, +}; export const useAuth = () => { const context = useContext(AuthContext); if (context === undefined) { - throw new Error("useAuth must be used within an AuthProvider"); + if (!warnedMissingProvider) { + console.warn("useAuth called without an AuthProvider. Falling back to safe defaults."); + warnedMissingProvider = true; + } + return missingProviderFallback; } return context; };