"use client" import { useState, useEffect } from "react" import { Loader2 } from "lucide-react" import { authClient } from "@/lib/auth-client" import { loginEmailSchema, loginSchema, } from "@/lib/auth/validation" import SocialButtons from "./social-buttons" import Link from "next/link" import { useRouter } from "next/navigation" export default function LoginForm() { const router = useRouter() const [phase, setPhase] = useState<"email" | "password">("email") const [email, setEmail] = useState("") const [password, setPassword] = useState("") const [error, setError] = useState("") const [isLoading, setIsLoading] = useState(false) // Preload passkeys for conditional UI useEffect(() => { if (phase === "password" && "PublicKeyCredential" in window) { authClient.signIn.passkey({ autoFill: true }) } }, [phase]) const handleEmailSubmit = async (e: React.FormEvent) => { e.preventDefault() setError("") const result = loginEmailSchema.safeParse({ email }) if (!result.success) { setError(result.error.issues[0].message) return } setIsLoading(true) try { const res = await fetch("/api/auth/check-email", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email }), }) if (!res.ok) { const data = await res.json() setError(data.error || "Something went wrong. Please try again.") return } const data = await res.json() if (!data.exists) { setError("No account found with this email.") return } // Email exists, move to password phase setPhase("password") } catch { setError("Something went wrong. Please try again.") } finally { setIsLoading(false) } } const handleLogin = async (e: React.FormEvent) => { e.preventDefault() setError("") const result = loginSchema.safeParse({ email, password }) if (!result.success) { setError(result.error.issues[0].message) return } setIsLoading(true) const { error } = await authClient.signIn.email({ email, password }) setIsLoading(false) if (error) { setError(error.message || "Invalid credentials. Please try again.") return } router.push("/") } const handleChangeEmail = () => { setPhase("email") setPassword("") setError("") } return (

Welcome back

{phase === "email" ? "Sign in to DeckyVault" : `Signing in as `} {phase === "password" && ( <> {email} {" "} )}

or continue with email
{error && (
{error} {error.includes("No account found") && ( <> {" "} Create one → )}
)} {phase === "email" ? (
setEmail(e.target.value)} placeholder="you@example.com" autoComplete="username webauthn" className="w-full px-4 py-3 rounded-lg border border-border bg-text/5 text-text text-sm placeholder:text-text/40 outline-none focus:border-primary focus:ring-2 focus:ring-primary/50 transition-colors" />
) : (
setPassword(e.target.value)} placeholder="Enter your password" autoComplete="current-password webauthn" autoFocus className="w-full px-4 py-3 rounded-lg border border-border bg-text/5 text-text text-sm placeholder:text-text/40 outline-none focus:border-primary focus:ring-2 focus:ring-primary/50 transition-colors" />
Forgot password?
{/* Passkey hint */}

Your browser may offer to sign in with a passkey

)}

Don't have an account?{" "} Create one

) }