"use client"; import Link from "next/link"; import { signIn } from "next-auth/react"; import { useTranslations } from "next-intl"; import { type FormEvent, useState } from "react"; import { precheckLogin } from "@/actions/auth-precheck"; import { ContentCard } from "@/components/public/ui"; export default function LoginPage() { const t = useTranslations("pages.login"); 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(t("errorInvalidCredentials")); return; } if (pre === "twofactor") { setNeeds2fa(true); return; } } const res = await signIn("credentials", { username, password, code, redirect: false, }); if (!res || res.error) { setError( needs2fa ? t("errorInvalid2fa") : t("errorInvalidCredentials"), ); return; } window.location.href = "/"; } finally { setPending(false); } } return (
setUsername(e.target.value)} placeholder={t("usernamePlaceholder")} autoComplete="username" disabled={needs2fa} /> setPassword(e.target.value)} placeholder={t("passwordPlaceholder")} autoComplete="current-password" disabled={needs2fa} /> {needs2fa ? ( setCode(e.target.value)} placeholder={t("codePlaceholder")} inputMode="numeric" autoComplete="one-time-code" // biome-ignore lint/a11y/noAutofocus: focus the only relevant field in the 2FA step autoFocus /> ) : null}
{!needs2fa ? ( <>
{t("or")}
) : null} {error ? (

{error}

) : null}

{t("noAccount")} {t("createOne")} ยท{" "} {t("forgotPassword")}

); }