From 20566e42ec0b39d9ce49191eadca6b9255479480 Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Tue, 9 Sep 2025 03:25:14 +0200 Subject: [PATCH] Fix sign in loading page --- web/pages/index.tsx | 1 + web/pages/signin.tsx | 43 +++++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/web/pages/index.tsx b/web/pages/index.tsx index 850b0dbb..f2885984 100644 --- a/web/pages/index.tsx +++ b/web/pages/index.tsx @@ -7,6 +7,7 @@ import {ProfilesHome} from "web/components/profiles/profiles-home"; export default function ProfilesPage() { const user = useUser(); + console.log('user:', user) return ( diff --git a/web/pages/signin.tsx b/web/pages/signin.tsx index 2653f4f3..793100bf 100644 --- a/web/pages/signin.tsx +++ b/web/pages/signin.tsx @@ -12,24 +12,7 @@ import {getLoverRow} from "common/love/lover"; import {db} from "web/lib/supabase/db"; import Router from "next/router"; import {LovePage} from "web/components/love-page"; - - -async function redirectSignin(creds: any) { - console.log("User signed in:", creds.user); - const userId = creds?.user.uid - await Router.push('/') - if (userId) { - try { - const lover = await getLoverRow(userId, db) - if (!lover) { - await Router.push('/signup') - } - } catch (error) { - console.error("Error fetching lover profile:", error); - } - } -} - +import {useUser} from "web/hooks/use-user"; export default function LoginPage() { return ( @@ -44,6 +27,7 @@ function RegisterComponent() { const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const [isLoadingGoogle, setIsLoadingGoogle] = useState(false); + const user = useUser() useEffect(() => { const error = searchParams.get('error'); @@ -54,12 +38,32 @@ function RegisterComponent() { } }, [searchParams]); + useEffect(() => { + const checkAndRedirect = async () => { + if (user) { + console.log("User signed in:", user); + try { + const lover = await getLoverRow(user.id, db) + if (lover) { + await Router.push('/') + } else { + await Router.push('/signup') + } + } catch (error) { + console.error("Error fetching lover profile:", error); + } + setIsLoading(false); + setIsLoadingGoogle(false); + } + } + checkAndRedirect() + }, [user]); + const handleGoogleSignIn = async () => { setIsLoadingGoogle(true); setError(null); try { const creds = await firebaseLogin(); - await redirectSignin(creds) } catch (error) { console.error("Error signing in:", error); const message = 'Failed to sign in with Google'; @@ -72,7 +76,6 @@ function RegisterComponent() { const handleEmailPasswordSignIn = async (email: string, password: string) => { try { const creds = await signInWithEmailAndPassword(auth, email, password); - await redirectSignin(creds) } catch (error) { console.error("Error signing in:", error); const message = 'Failed to sign in with your email and password';