"use client" import { useState } from "react" import { Mail, KeyRound, ArrowLeft, Loader2, CheckCircle } from "lucide-react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { useAuth } from "@/lib/auth" import { useTranslations } from "next-intl" interface AuthDialogProps { open: boolean onClose: () => void onSuccess: () => void } type AuthStep = "email" | "sent" | "token" export function AuthDialog({ open, onClose, onSuccess }: AuthDialogProps) { const t = useTranslations('auth.dialog') const tCommon = useTranslations('common') const [step, setStep] = useState("email") const [email, setEmail] = useState("") const [token, setToken] = useState("") const [localError, setLocalError] = useState("") const { sendMagicLink, loginWithToken, isLoading, error, clearError } = useAuth() const handleSendMagicLink = async (e: React.FormEvent) => { e.preventDefault() setLocalError("") if (!email.trim()) { setLocalError("Please enter your email") return } // Simple email validation if (!email.includes("@") || !email.includes(".")) { setLocalError("Please enter a valid email") return } const success = await sendMagicLink(email.trim().toLowerCase()) if (success) { setStep("sent") } } const handleTokenSubmit = async (e: React.FormEvent) => { e.preventDefault() setLocalError("") if (!token.trim()) { setLocalError("Please enter your access token") return } const success = await loginWithToken(token.trim()) if (success) { setToken("") // Clear for security onSuccess() } } const handleClose = () => { setStep("email") setEmail("") setToken("") setLocalError("") clearError() onClose() } const displayError = localError || error return ( !open && handleClose()}> {step === "email" && ( <> {t('title')} {t('emailPrompt')}
{ setEmail(e.target.value) setLocalError("") clearError() }} className="bg-secondary border-border text-foreground" autoFocus disabled={isLoading} /> {displayError && ( displayError === "disposable_email_not_allowed" ? (

{t('disposableEmailWarning')}

{t('disposableEmailExplanation')}

{t('noSpamPromise')}

) : (

{displayError}

) )}

{t('noAccount')}

)} {step === "sent" && ( <> {t('checkEmailTitle')}

{t('checkEmailSent')}

{email}

{t('checkEmailExpiry')}

)} {step === "token" && ( <> {t('accessTokenTitle')} {t('accessTokenPrompt')}
{ setToken(e.target.value) setLocalError("") clearError() }} className="bg-secondary border-border text-foreground" autoFocus disabled={isLoading} /> {displayError && (

{displayError}

)}

{t('contactAdmin')}

)}
) }