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:
@@ -69,6 +69,9 @@ export default function Auth() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { error, data } = await signUp(email, password, name);
|
const { error, data } = await signUp(email, password, name);
|
||||||
|
|
||||||
|
console.log('SignUp result:', { error, data, hasUser: !!data?.user, hasSession: !!data?.session });
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
if (error.message.includes('already registered')) {
|
if (error.message.includes('already registered')) {
|
||||||
toast({ title: 'Error', description: 'This email is already registered. Please login instead.', variant: 'destructive' });
|
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' });
|
toast({ title: 'Error', description: error.message, variant: 'destructive' });
|
||||||
}
|
}
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
} else if (data?.user) {
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user