@@ -12,24 +12,7 @@ import {getLoverRow} from "common/love/lover";
1212import { db } from "web/lib/supabase/db" ;
1313import Router from "next/router" ;
1414import { LovePage } from "web/components/love-page" ;
15-
16-
17- async function redirectSignin ( creds : any ) {
18- console . log ( "User signed in:" , creds . user ) ;
19- const userId = creds ?. user . uid
20- await Router . push ( '/' )
21- if ( userId ) {
22- try {
23- const lover = await getLoverRow ( userId , db )
24- if ( ! lover ) {
25- await Router . push ( '/signup' )
26- }
27- } catch ( error ) {
28- console . error ( "Error fetching lover profile:" , error ) ;
29- }
30- }
31- }
32-
15+ import { useUser } from "web/hooks/use-user" ;
3316
3417export default function LoginPage ( ) {
3518 return (
@@ -44,6 +27,7 @@ function RegisterComponent() {
4427 const [ error , setError ] = useState < string | null > ( null ) ;
4528 const [ isLoading , setIsLoading ] = useState ( false ) ;
4629 const [ isLoadingGoogle , setIsLoadingGoogle ] = useState ( false ) ;
30+ const user = useUser ( )
4731
4832 useEffect ( ( ) => {
4933 const error = searchParams . get ( 'error' ) ;
@@ -54,12 +38,32 @@ function RegisterComponent() {
5438 }
5539 } , [ searchParams ] ) ;
5640
41+ useEffect ( ( ) => {
42+ const checkAndRedirect = async ( ) => {
43+ if ( user ) {
44+ console . log ( "User signed in:" , user ) ;
45+ try {
46+ const lover = await getLoverRow ( user . id , db )
47+ if ( lover ) {
48+ await Router . push ( '/' )
49+ } else {
50+ await Router . push ( '/signup' )
51+ }
52+ } catch ( error ) {
53+ console . error ( "Error fetching lover profile:" , error ) ;
54+ }
55+ setIsLoading ( false ) ;
56+ setIsLoadingGoogle ( false ) ;
57+ }
58+ }
59+ checkAndRedirect ( )
60+ } , [ user ] ) ;
61+
5762 const handleGoogleSignIn = async ( ) => {
5863 setIsLoadingGoogle ( true ) ;
5964 setError ( null ) ;
6065 try {
6166 const creds = await firebaseLogin ( ) ;
62- await redirectSignin ( creds )
6367 } catch ( error ) {
6468 console . error ( "Error signing in:" , error ) ;
6569 const message = 'Failed to sign in with Google' ;
@@ -72,7 +76,6 @@ function RegisterComponent() {
7276 const handleEmailPasswordSignIn = async ( email : string , password : string ) => {
7377 try {
7478 const creds = await signInWithEmailAndPassword ( auth , email , password ) ;
75- await redirectSignin ( creds )
7679 } catch ( error ) {
7780 console . error ( "Error signing in:" , error ) ;
7881 const message = 'Failed to sign in with your email and password' ;
0 commit comments