From 645fdec8b21f6c068176efe36b89565c0b413859 Mon Sep 17 00:00:00 2001 From: Aditya30ag Date: Wed, 9 Jul 2025 11:12:57 +0530 Subject: [PATCH] Fix Google OAuth onboarding redirection issue - Fixed TypeScript linter error in checkUserOnboarding function - Updated onAuthStateChange listener to check onboarding status and redirect appropriately - Added initial session check to handle direct navigation scenarios - Removed direct navigation from login() function to prevent conflicts - Users now properly redirected to onboarding when incomplete or dashboard when complete --- Frontend/src/context/AuthContext.tsx | 98 +++++++++++++++++++++------- 1 file changed, 74 insertions(+), 24 deletions(-) diff --git a/Frontend/src/context/AuthContext.tsx b/Frontend/src/context/AuthContext.tsx index 8588c41..f6509d6 100644 --- a/Frontend/src/context/AuthContext.tsx +++ b/Frontend/src/context/AuthContext.tsx @@ -81,29 +81,34 @@ export const AuthProvider = ({ children }: AuthProviderProps) => { } setLastRequest(now); - // Check if user has completed onboarding by looking for social profiles or brand data - const { data: socialProfiles } = await supabase - .from("social_profiles") - .select("id") - .eq("user_id", userToUse.id) - .limit(1); - - const { data: brandData } = await supabase - .from("brands") - .select("id") - .eq("user_id", userToUse.id) - .limit(1); - - const hasOnboarding = (socialProfiles && socialProfiles.length > 0) || (brandData && brandData.length > 0); - - // Get user role - const { data: userData } = await supabase - .from("users") - .select("role") - .eq("id", userToUse.id) - .single(); - - return { hasOnboarding, role: userData?.role || null }; + try { + // Check if user has completed onboarding by looking for social profiles or brand data + const { data: socialProfiles } = await supabase + .from("social_profiles") + .select("id") + .eq("user_id", userToUse.id) + .limit(1); + + const { data: brandData } = await supabase + .from("brands") + .select("id") + .eq("user_id", userToUse.id) + .limit(1); + + const hasOnboarding = Boolean((socialProfiles && socialProfiles.length > 0) || (brandData && brandData.length > 0)); + + // Get user role + const { data: userData } = await supabase + .from("users") + .select("role") + .eq("id", userToUse.id) + .single(); + + return { hasOnboarding, role: userData?.role || null }; + } catch (error) { + console.error("Error checking onboarding status:", error); + return { hasOnboarding: false, role: null }; + } }; useEffect(() => { @@ -135,6 +140,30 @@ export const AuthProvider = ({ children }: AuthProviderProps) => { console.log("AuthContext: Ensuring user in table"); try { await ensureUserInTable(data.session.user); + + // Check onboarding status and redirect appropriately for initial load + // Only redirect if not on reset-password page and not already on a dashboard/onboarding page + const currentPath = window.location.pathname; + if (!currentPath.includes('/reset-password') && + !currentPath.includes('/dashboard') && + !currentPath.includes('/onboarding')) { + console.log("AuthContext: Checking onboarding status for initial redirection"); + const { hasOnboarding, role } = await checkUserOnboarding(data.session.user); + console.log("AuthContext: Initial onboarding check result", { hasOnboarding, role }); + + if (hasOnboarding) { + if (role === "brand") { + console.log("AuthContext: Initial redirect to brand dashboard"); + navigate("/brand/dashboard"); + } else { + console.log("AuthContext: Initial redirect to dashboard"); + navigate("/dashboard"); + } + } else { + console.log("AuthContext: Initial redirect to onboarding"); + navigate("/onboarding"); + } + } } catch (error) { console.error("AuthContext: Error ensuring user in table", error); } @@ -161,6 +190,27 @@ export const AuthProvider = ({ children }: AuthProviderProps) => { console.log("AuthContext: User authenticated"); try { await ensureUserInTable(session.user); + + // Check onboarding status and redirect appropriately + // Only redirect on SIGNED_IN event to avoid conflicts during password reset + if (event === 'SIGNED_IN' && !window.location.pathname.includes('/reset-password')) { + console.log("AuthContext: Checking onboarding status for redirection"); + const { hasOnboarding, role } = await checkUserOnboarding(session.user); + console.log("AuthContext: Onboarding check result", { hasOnboarding, role }); + + if (hasOnboarding) { + if (role === "brand") { + console.log("AuthContext: Redirecting to brand dashboard"); + navigate("/brand/dashboard"); + } else { + console.log("AuthContext: Redirecting to dashboard"); + navigate("/dashboard"); + } + } else { + console.log("AuthContext: Redirecting to onboarding"); + navigate("/onboarding"); + } + } } catch (error) { console.error("AuthContext: Error ensuring user in table", error); } @@ -182,7 +232,7 @@ export const AuthProvider = ({ children }: AuthProviderProps) => { const login = () => { setIsAuthenticated(true); - navigate("/dashboard"); + // Navigation is now handled by the onAuthStateChange listener }; const logout = async () => {