auth redirect updated for portal
This commit is contained in:
parent
7b897e3672
commit
bce0c5063b
5 changed files with 85 additions and 120 deletions
|
|
@ -3,21 +3,8 @@ import { cookies } from "next/headers";
|
|||
import { redirect } from "next/navigation";
|
||||
import SignUp from "./sign-up";
|
||||
|
||||
export default async function SignUpPage({
|
||||
searchParams,
|
||||
}: {
|
||||
searchParams?: Record<string, string | string[] | undefined>;
|
||||
}) {
|
||||
export default async function SignUpPage() {
|
||||
const at = (await cookies()).get("ma_at")?.value;
|
||||
if (at) {
|
||||
redirect("/portal");
|
||||
}
|
||||
|
||||
const nextParam = toStr(searchParams?.next) || "/portal";
|
||||
return <SignUp nextPath={nextParam} />;
|
||||
}
|
||||
|
||||
function toStr(v: string | string[] | undefined): string | undefined {
|
||||
if (!v) return undefined;
|
||||
return Array.isArray(v) ? v[0] : v;
|
||||
if (at) redirect("/portal");
|
||||
return <SignUp nextPath="/portal" />;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,15 +4,12 @@
|
|||
import { useState, useCallback } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
type Props = {
|
||||
nextPath?: string; // where to go after successful sign-up
|
||||
};
|
||||
type Props = { nextPath?: string };
|
||||
|
||||
export default function SignUp({ nextPath = "/portal" }: Props) {
|
||||
const router = useRouter();
|
||||
|
||||
const [username, setUsername] = useState("");
|
||||
const [email, setEmail] = useState(""); // optional per your backend flow
|
||||
const [email, setEmail] = useState(""); // optional if your backend allows
|
||||
const [password, setPassword] = useState("");
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
|
@ -28,11 +25,7 @@ export default function SignUp({ nextPath = "/portal" }: Props) {
|
|||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
email: email || undefined,
|
||||
password,
|
||||
}),
|
||||
body: JSON.stringify({ username, email: email || undefined, password }),
|
||||
});
|
||||
|
||||
const txt = await res.text();
|
||||
|
|
@ -40,16 +33,11 @@ export default function SignUp({ nextPath = "/portal" }: Props) {
|
|||
try { j = txt ? JSON.parse(txt) : null; } catch {}
|
||||
|
||||
if (!res.ok) {
|
||||
const message =
|
||||
j?.error ||
|
||||
j?.message ||
|
||||
(typeof j === "string" ? j : "") ||
|
||||
`Sign-up failed (${res.status})`;
|
||||
const message = j?.error || j?.message || `Sign-up failed (${res.status})`;
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
// Expect server to create user + set cookies (ma_at, etc.)
|
||||
router.replace(nextPath || "/portal");
|
||||
router.replace(nextPath); // ALWAYS /portal
|
||||
router.refresh();
|
||||
} catch (e: any) {
|
||||
setErr(e?.message || "Unable to sign up.");
|
||||
|
|
@ -129,9 +117,7 @@ export default function SignUp({ nextPath = "/portal" }: Props) {
|
|||
|
||||
<div className="mt-4 text-center text-sm">
|
||||
<span className="opacity-70">Already have an account?</span>{" "}
|
||||
<a className="underline" href={`/auth/sign-in?next=${encodeURIComponent(nextPath)}`}>
|
||||
Sign in
|
||||
</a>
|
||||
<a className="underline" href={"/auth/sign-in"}>Sign in</a>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue