From 079c0f947c3cdd22a37630a7761e9e4585a26c02 Mon Sep 17 00:00:00 2001 From: dwindown Date: Fri, 2 Jan 2026 14:34:09 +0700 Subject: [PATCH] Improve auth flow error handling and add debug logging - Add early returns for better error handling flow - Add console.log for SignUp result to debug user creation - Ensure loading state is always reset properly - Add explicit check for missing user data after signUp --- src/pages/Auth.tsx | 52 +++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/pages/Auth.tsx b/src/pages/Auth.tsx index 470c89c..f3eabab 100644 --- a/src/pages/Auth.tsx +++ b/src/pages/Auth.tsx @@ -69,6 +69,9 @@ export default function Auth() { } const { error, data } = await signUp(email, password, name); + + console.log('SignUp result:', { error, data, hasUser: !!data?.user, hasSession: !!data?.session }); + if (error) { if (error.message.includes('already registered')) { toast({ title: 'Error', description: 'This email is already registered. Please login instead.', variant: 'destructive' }); @@ -76,28 +79,35 @@ export default function Auth() { toast({ title: 'Error', description: error.message, variant: 'destructive' }); } setLoading(false); - } else if (data?.user) { - // User created, now send OTP - const userId = data.user.id; - console.log('User created successfully:', { userId, email, session: data.session }); - - const result = await sendAuthOTP(userId, email); - - console.log('OTP send result:', result); - - if (result.success) { - setPendingUserId(userId); - setShowOTP(true); - setResendCountdown(60); // 60 seconds cooldown - toast({ - title: 'OTP Terkirim', - description: 'Kode verifikasi telah dikirim ke email Anda. Silakan cek inbox.', - }); - } else { - toast({ title: 'Error', description: result.message, variant: 'destructive' }); - } - setLoading(false); + return; } + + if (!data?.user) { + toast({ title: 'Error', description: 'Failed to create user account. Please try again.', variant: 'destructive' }); + setLoading(false); + return; + } + + // User created, now send OTP + const userId = data.user.id; + console.log('User created successfully:', { userId, email, session: data.session }); + + const result = await sendAuthOTP(userId, email); + + console.log('OTP send result:', result); + + if (result.success) { + setPendingUserId(userId); + setShowOTP(true); + setResendCountdown(60); // 60 seconds cooldown + toast({ + title: 'OTP Terkirim', + description: 'Kode verifikasi telah dikirim ke email Anda. Silakan cek inbox.', + }); + } else { + toast({ title: 'Error', description: result.message, variant: 'destructive' }); + } + setLoading(false); } };