"use client"; import { authClient } from "@/lib/auth-client"; import { useRouter } from "next/navigation"; import { useRef, useState } from "react"; const OTP_STEP_STORAGE_KEY = "hellthhub_login_step_otp"; function clearOtpStep() { try { window.localStorage.removeItem(OTP_STEP_STORAGE_KEY); } catch { // ignore storage access issues } } export default function OtpLoginPage() { const router = useRouter(); const inputRef = useRef(null); const [method, setMethod] = useState<"totp" | "backup">("totp"); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); setLoading(true); try { const code = inputRef.current?.value?.trim() ?? ""; const normalizedCode = method === "totp" ? code.replace(/\D/g, "") : code; if (method === "totp" && normalizedCode.length !== 6) { setError("Bitte den 6-stelligen OTP-Code eingeben."); return; } if (method === "backup" && normalizedCode.length < 6) { setError("Bitte einen gültigen Backup-Code eingeben."); return; } const verifyResult = method === "totp" ? await authClient.twoFactor.verifyTotp({ code: normalizedCode }) : await authClient.twoFactor.verifyBackupCode({ code: normalizedCode }); if (verifyResult.error) { const message = verifyResult.error.message ?? (method === "totp" ? "OTP-Code ungültig. Bitte erneut prüfen." : "Backup-Code ungültig. Bitte erneut prüfen."); if ( message.includes("session") || message.includes("Session") || message.includes("expired") ) { setError( "OTP-Sitzung abgelaufen. Bitte erneut mit E-Mail und Passwort anmelden.", ); return; } setError( method === "totp" ? "OTP-Code ungültig. Bitte erneut prüfen." : "Backup-Code ungültig. Bitte erneut prüfen.", ); return; } clearOtpStep(); if (inputRef.current) { inputRef.current.value = ""; } router.push("/"); } catch { setError("2FA-Prüfung derzeit nicht möglich. Bitte erneut versuchen."); } finally { setLoading(false); } } return (
HH

Hellth Hub

OTP-Verifizierung

{error && (
! {error}
)}
{ const input = e.currentTarget; if (method === "totp") { input.value = input.value.replace(/\D/g, "").slice(0, 6); } }} placeholder={method === "totp" ? "123456" : "z. B. ABCD-EFGH"} className="w-full px-3 py-2.5 border border-gray-200 rounded-lg text-sm text-[#333333] placeholder-gray-300 focus:outline-none focus:ring-2 focus:ring-[#0F766E] focus:border-transparent transition-shadow" /> {method === "backup" && (

Jeder Backup-Code ist nur einmal gültig.

)}
); }