'use client'; import { useState, useEffect } from 'react'; import { signIn } from 'next-auth/react'; import { useRouter, useSearchParams } from 'next/navigation'; import { Eye, EyeOff } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import Link from 'next/link'; import { ForgotPasswordDialog } from '@/components/ForgotPasswordDialog'; interface LoginDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onSuccess?: () => void; onOpenRegister?: () => void; callbackUrl?: string; registered?: boolean; } export function LoginDialog({ open, onOpenChange, onSuccess, onOpenRegister, callbackUrl: initialCallbackUrl, registered: initialRegistered, }: LoginDialogProps) { const router = useRouter(); const searchParams = useSearchParams(); const callbackUrl = initialCallbackUrl || searchParams.get('callbackUrl') || '/'; const registered = initialRegistered || searchParams.get('registered') === 'true'; const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const [emailNotVerified, setEmailNotVerified] = useState(false); const [isLoading, setIsLoading] = useState(false); const [isResending, setIsResending] = useState(false); const [forgotPasswordOpen, setForgotPasswordOpen] = useState(false); const [showPassword, setShowPassword] = useState(false); // Reset all form state when dialog opens useEffect(() => { if (open) { setEmail(''); setPassword(''); setError(''); setEmailNotVerified(false); setIsLoading(false); setIsResending(false); setShowPassword(false); } }, [open]); const handleResendConfirmation = async () => { setIsResending(true); try { const response = await fetch('/api/auth/resend-confirmation', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }), }); const data = await response.json(); if (response.ok) { setError(''); setEmailNotVerified(false); alert('Confirmation email sent! Please check your inbox.'); } else { alert(data.error || 'Failed to resend confirmation email'); } } catch (err) { alert('An error occurred. Please try again.'); } finally { setIsResending(false); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); setEmailNotVerified(false); setIsLoading(true); try { // First check if email is verified const checkResponse = await fetch('/api/auth/check-verification', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password }), }); const checkData = await checkResponse.json(); if (!checkData.exists) { setError('Invalid email or password'); setIsLoading(false); return; } if (!checkData.passwordValid) { setError('Invalid email or password'); setIsLoading(false); return; } if (!checkData.verified) { setEmailNotVerified(true); setIsLoading(false); return; } // Email is verified, proceed with login const result = await signIn('credentials', { email, password, redirect: false, }); if (result?.error) { setError('Invalid email or password'); } else { onOpenChange(false); if (onSuccess) { onSuccess(); } else { router.push(callbackUrl); router.refresh(); } } } catch (err) { setError('An error occurred. Please try again.'); } finally { setIsLoading(false); } }; const handleOpenChange = (newOpen: boolean) => { if (!newOpen) { // Reset form when closing setEmail(''); setPassword(''); setError(''); setEmailNotVerified(false); setIsResending(false); } onOpenChange(newOpen); }; return ( Sign in to your account Or{' '} {onOpenRegister ? ( ) : ( handleOpenChange(false)} > create a new account )}
{registered && (

Account created successfully! Please check your email to confirm your account before signing in.

)} {searchParams.get('verified') === 'true' && (

Email verified successfully! You can now sign in.

)} {emailNotVerified && (

Please verify your email address before signing in. Check your inbox for a confirmation email.

)} {error && (

{error}

)}
{ setEmail(e.target.value); // Clear email verification error when email changes if (emailNotVerified) { setEmailNotVerified(false); } }} className="mt-1" placeholder="you@example.com" />
setPassword(e.target.value)} className="pr-10" placeholder="••••••••" />
); }