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
This commit is contained in:
dwindown
2026-01-02 14:34:09 +07:00
parent 06d6845456
commit 079c0f947c

View File

@@ -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);
}
};