Fix email unconfirmed login flow with OTP resend and update email API field names
This commit is contained in:
@@ -23,7 +23,7 @@ export default function Auth() {
|
||||
const [pendingUserId, setPendingUserId] = useState<string | null>(null);
|
||||
const [resendCountdown, setResendCountdown] = useState(0);
|
||||
const [isResendOTP, setIsResendOTP] = useState(false); // Track if this is resend OTP for existing user
|
||||
const { signIn, signUp, user, sendAuthOTP, verifyAuthOTP } = useAuth();
|
||||
const { signIn, signUp, user, sendAuthOTP, verifyAuthOTP, getUserByEmail } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -58,18 +58,42 @@ export default function Auth() {
|
||||
if (isLogin) {
|
||||
const { error } = await signIn(email, password);
|
||||
if (error) {
|
||||
console.log('Login error:', error.message);
|
||||
|
||||
// Check if error is due to unconfirmed email
|
||||
if (error.message.includes('Email not confirmed') || error.message.includes('Email not verified')) {
|
||||
toast({
|
||||
title: 'Email Belum Dikonfirmasi',
|
||||
description: 'Kirim ulang kode verifikasi ke email Anda?',
|
||||
variant: 'destructive'
|
||||
});
|
||||
// Switch to OTP resend flow
|
||||
setIsResendOTP(true);
|
||||
setShowOTP(true);
|
||||
// Get user ID from error or fetch it
|
||||
// For now, we'll handle it in the OTP resend button
|
||||
// Supabase returns various error messages for unconfirmed email
|
||||
const isUnconfirmedEmail =
|
||||
error.message.includes('Email not confirmed') ||
|
||||
error.message.includes('Email not verified') ||
|
||||
error.message.includes('Email not confirmed') ||
|
||||
error.message.toLowerCase().includes('email') && error.message.toLowerCase().includes('not confirmed') ||
|
||||
error.message.toLowerCase().includes('unconfirmed');
|
||||
|
||||
console.log('Is unconfirmed email?', isUnconfirmedEmail);
|
||||
|
||||
if (isUnconfirmedEmail) {
|
||||
// Get user by email to fetch user_id
|
||||
console.log('Fetching user by email for OTP resend...');
|
||||
const userResult = await getUserByEmail(email);
|
||||
|
||||
console.log('User lookup result:', userResult);
|
||||
|
||||
if (userResult.success && userResult.user_id) {
|
||||
setPendingUserId(userResult.user_id);
|
||||
setIsResendOTP(true);
|
||||
setShowOTP(true);
|
||||
setResendCountdown(0); // Allow immediate resend on first attempt
|
||||
toast({
|
||||
title: 'Email Belum Dikonfirmasi',
|
||||
description: 'Silakan verifikasi email Anda. Kami akan mengirimkan kode OTP.',
|
||||
});
|
||||
} else {
|
||||
toast({
|
||||
title: 'Error',
|
||||
description: 'User tidak ditemukan. Silakan daftar terlebih dahulu.',
|
||||
variant: 'destructive'
|
||||
});
|
||||
}
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user