"use client"; import { useI18n } from "@/components/I18nProvider"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { ArrowRight, Compass, Mail, ShieldCheck } from "lucide-react"; import { api } from "./StartGame"; import { authActionSchema, authDestination } from "@/shared/account"; type Mode = "login" | "signup" | "recover" | "magic"; export function SignIn({ next = "/dashboard", initialError = "", }: { next?: string; initialError?: string; }) { const { t } = useI18n(); const [mode, setMode] = useState("login"), [email, setEmail] = useState(""), [name, setName] = useState(""), [password, setPassword] = useState(""), [confirmation, setConfirmation] = useState(""), [message, setMessage] = useState(""), [error, setError] = useState(initialError), [busy, setBusy] = useState(false); const router = useRouter(); function changeMode(value: Mode) { setMode(value); setError(""); setMessage(""); setPassword(""); setConfirmation(""); } async function submit(event: React.FormEvent) { event.preventDefault(); setError(""); setMessage(""); const body = mode === "signup" ? { action: mode, email, password, displayName: name, next } : mode === "login" ? { action: mode, email, password, next } : mode === "magic" ? { action: mode, email, next } : { action: mode, email }; const valid = authActionSchema.safeParse(body); if (!valid.success) { setError(valid.error.issues[0].message); return; } if (mode === "signup" && password !== confirmation) { setError("Your passwords do not match."); return; } setBusy(true); try { const result = await api<{ next?: string; message?: string }>( "auth", valid.data, ); setPassword(""); setConfirmation(""); if (result.next) { router.replace(authDestination(result.next)); router.refresh(); } else setMessage(result.message ?? "Check your email to continue."); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } const title = { login: "Welcome back.", signup: "Make yourself at home.", recover: "Find your way back.", magic: "An email, a way in.", }[mode]; return (
{t("YOUR CARTOGRAPHER ACCOUNT")}

{t(title)}

{mode === "signup" ? t("Choose the name people will see on your maps.") : mode === "recover" ? t("We’ll email you a link to set a new password.") : mode === "magic" ? t( "For existing accounts. Open the link on this device to continue.", ) : t("Log in with your email and password.")}

{mode === "signup" && ( )} {(mode === "login" || mode === "signup") && ( )} {mode === "signup" && ( )} {mode === "login" && ( )} {error && (

{t(error)}

)} {message && (

{t(message)}

)}
{mode === "login" && (
{t("Already use email links?")}
)} {(mode === "recover" || mode === "magic") && ( )}

{t( "Your email stays private. Your display name appears with your maps.", )}

); }