Refactor: Extract master template to shared file and add unconfirmed email handling

- Create supabase/shared/email-template-renderer.ts for code reuse
- Update send-auth-otp to import from shared file (eliminates 260 lines of duplication)
- Add isResendOTP state to track existing user email confirmation
- Update login error handler to detect unconfirmed email
- Show helpful message when user tries to login with unconfirmed email

This addresses:
1. Code duplication between src/lib and edge functions
2. User experience for unconfirmed email login attempts
This commit is contained in:
dwindown
2026-01-02 17:16:59 +07:00
parent da9a68f084
commit dafa4eeeb3
3 changed files with 271 additions and 254 deletions

View File

@@ -22,6 +22,7 @@ export default function Auth() {
const [loading, setLoading] = useState(false);
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 navigate = useNavigate();
@@ -57,9 +58,26 @@ export default function Auth() {
if (isLogin) {
const { error } = await signIn(email, password);
if (error) {
// 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
setLoading(false);
return;
}
toast({ title: 'Error', description: error.message, variant: 'destructive' });
setLoading(false);
} else {
navigate('/dashboard');
setLoading(false);
}
} else {
if (!name.trim()) {