"use client"; import { Suspense, useState } from "react"; import Link from "next/link"; import { useSearchParams, useRouter } from "next/navigation"; import AuthShell from "@/components/AuthShell"; import { api, setToken } from "@/lib/api"; import { useLanguage } from "@/contexts/LanguageContext"; function Inner() { const sp = useSearchParams(); const router = useRouter(); const { setLang } = useLanguage(); const email = sp.get("email") ?? ""; const [otp, setOtp] = useState(""); const [err, setErr] = useState(""); const [msg, setMsg] = useState(""); const [busy, setBusy] = useState(false); async function submit(e: React.FormEvent) { e.preventDefault(); setBusy(true); setErr(""); try { const res = await api("/auth/verify-login-otp", { method: "POST", body: JSON.stringify({ email, otp, }), }); setToken(res.access_token); const org = await api("/organization"); if (org.language) { setLang(org.language); localStorage.setItem("language", org.language); } if (org.timezone) { localStorage.setItem("timezone", org.timezone); } const me = await api("/me"); if (me.is_product_admin) { router.push("/admin"); } else if (!me.has_org) { router.push("/onboarding"); } else { router.push("/dashboard"); } } catch (e: any) { setErr( typeof e.detail === "string" ? e.detail : "Invalid or expired OTP" ); } finally { setBusy(false); } } async function resend() { setErr(""); setMsg(""); try { await api("/auth/login", { method: "POST", body: JSON.stringify({ email, resend: true, }), }); setMsg("A new OTP has been sent."); } catch { setErr("Unable to resend OTP."); } } return ( Login OTP setOtp( e.target.value .replace(/\D/g, "") .slice(0, 6) ) } inputMode="numeric" placeholder="123456" className="mt-1 w-full text-center tracking-[0.5em] text-lg px-3 py-3 rounded-lg border border-slate-200 focus:outline-none focus:ring-2 focus:ring-brand-500/30" /> {err && ( {err} )} {msg && ( {msg} )} {busy ? "Verifying..." : "Verify OTP"} Resend OTP Back to login Check your email inbox (and spam folder) for the login OTP. ); } export default function LoginOTP() { return ( ); }
{err}
{msg}
Check your email inbox (and spam folder) for the login OTP.