From ae528d8f22e2fd5d890406d1ab7003256b831881 Mon Sep 17 00:00:00 2001 From: makearmy Date: Tue, 30 Sep 2025 22:14:50 -0400 Subject: [PATCH] registration fix --- app/auth/sign-up/page.tsx | 17 ++++- app/auth/sign-up/sign-up.tsx | 121 ++++++++++++++++------------------- 2 files changed, 68 insertions(+), 70 deletions(-) diff --git a/app/auth/sign-up/page.tsx b/app/auth/sign-up/page.tsx index 7c3b350e..569ae57c 100644 --- a/app/auth/sign-up/page.tsx +++ b/app/auth/sign-up/page.tsx @@ -3,8 +3,19 @@ import { cookies } from "next/headers"; import { redirect } from "next/navigation"; import SignUp from "./sign-up"; -export default async function SignUpPage() { - const at = (await cookies()).get("ma_at")?.value; +export default async function SignUpPage({ + searchParams, +}: { + searchParams?: Record; +}) { + const ck = await cookies(); + const at = ck.get("ma_at")?.value; if (at) redirect("/portal"); - return ; + + const sp = searchParams ?? {}; + const nextParam = Array.isArray(sp.next) ? sp.next[0] : sp.next; + const nextPath = + nextParam && String(nextParam).startsWith("/") ? String(nextParam) : "/portal"; + + return ; } diff --git a/app/auth/sign-up/sign-up.tsx b/app/auth/sign-up/sign-up.tsx index f41246f9..76137a96 100644 --- a/app/auth/sign-up/sign-up.tsx +++ b/app/auth/sign-up/sign-up.tsx @@ -15,36 +15,60 @@ export default function SignUp({ nextPath = "/portal" }: Props) { const [loading, setLoading] = useState(false); const [err, setErr] = useState(null); - const onSubmit = useCallback(async (e: React.FormEvent) => { - e.preventDefault(); - setErr(null); - setLoading(true); + const onSubmit = useCallback( + async (e: React.FormEvent) => { + e.preventDefault(); + setErr(null); - try { - const res = await fetch("/api/auth/register", { - method: "POST", - credentials: "include", - headers: { "Content-Type": "application/json", Accept: "application/json" }, - body: JSON.stringify({ username, email: email || undefined, password }), - }); + const u = username.trim(); + const em = email.trim().toLowerCase(); + const pw = password; - const txt = await res.text(); - let j: any = null; - try { j = txt ? JSON.parse(txt) : null; } catch {} - - if (!res.ok) { - const message = j?.error || j?.message || `Sign-up failed (${res.status})`; - throw new Error(message); + if (!u) { + setErr("Username is required."); + return; + } + if (!pw || pw.length < 8) { + setErr("Password must be at least 8 characters."); + return; } - router.replace(nextPath); // ALWAYS /portal - router.refresh(); - } catch (e: any) { - setErr(e?.message || "Unable to sign up."); - } finally { - setLoading(false); - } - }, [username, email, password, nextPath, router]); + setLoading(true); + try { + const res = await fetch("/api/auth/register", { + method: "POST", + credentials: "include", + headers: { "Content-Type": "application/json", Accept: "application/json" }, + body: JSON.stringify({ username: u, email: em || undefined, password: pw }), + }); + + const txt = await res.text(); + let j: any = null; + try { + j = txt ? JSON.parse(txt) : null; + } catch { + j = null; + } + + if (!res.ok) { + const message = + (j?.error && j?.debug ? `${j.error} (${j.debug})` : j?.error) || + j?.message || + `Sign-up failed (${res.status})`; + throw new Error(message); + } + + // If the server has AUTO_LOGIN on, user is already signed in here. + router.replace(nextPath); + router.refresh(); + } catch (e: any) { + setErr(e?.message || "Unable to sign up."); + } finally { + setLoading(false); + } + }, + [username, email, password, nextPath, router] + ); return (
@@ -66,7 +90,9 @@ export default function SignUp({ nextPath = "/portal" }: Props) {
- + Password -
- setPassword(e.currentTarget.value)} - required - minLength={8} - /> - - - {err && ( -
- {err} -
- )} - - - - -
- Already have an account?{" "} - Sign in -
- - ); -} + className="text-xs opaci