From d79788aa57dc1ae63dd7e7fa3f67559f7e4276ff Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Tue, 30 Sep 2025 04:15:05 +0000 Subject: [PATCH] Make useAuth resilient when AuthProvider is missing (fallback) cgen-4d84a008018249a9b15d223d104ac336 --- client/contexts/AuthContext.tsx | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) 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; };