"use client"; import { signIn } from "next-auth/react"; import { type FormEvent, useState } from "react"; import { precheckLogin } from "@/actions/auth-precheck"; export function HomeLoginForm() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [code, setCode] = useState(""); const [needs2fa, setNeeds2fa] = useState(false); const [error, setError] = useState(null); const [pending, setPending] = useState(false); async function onSubmit(e: FormEvent) { e.preventDefault(); setError(null); setPending(true); try { if (!needs2fa) { const pre = await precheckLogin(username, password); if (pre === "invalid") { setError("Invalid username or password"); return; } if (pre === "twofactor") { setNeeds2fa(true); return; } } const res = await signIn("credentials", { username, password, code, redirect: false, }); if (!res || res.error) { setError(needs2fa ? "Invalid 2FA code" : "Invalid username or password"); return; } window.location.href = "/"; } finally { setPending(false); } } return (
setUsername(e.target.value)} placeholder="" className="focus:ring-0 relative z-10 w-full rounded border-4 border-gray-200 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-200" style={{ borderColor: "#e5e7eb", borderWidth: "4px" }} required disabled={needs2fa} />
setPassword(e.target.value)} placeholder="" className="focus:ring-0 relative z-10 w-full rounded border-4 border-gray-200 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-200" style={{ borderColor: "#e5e7eb", borderWidth: "4px" }} required disabled={needs2fa} />
{needs2fa ? (
setCode(e.target.value)} placeholder="Enter your 2FA code" inputMode="numeric" className="focus:ring-0 relative z-10 w-full rounded border-4 border-gray-200 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-200" style={{ borderColor: "#e5e7eb", borderWidth: "4px" }} autoFocus />
) : null} {error ?

{error}

: null}
); }