diff --git a/src/routes/_auth.setup.tsx b/src/routes/_auth.setup.tsx index 6f0fd06..74a8024 100644 --- a/src/routes/_auth.setup.tsx +++ b/src/routes/_auth.setup.tsx @@ -1,5 +1,5 @@ -import { createFileRoute, redirect, useNavigate } from "@tanstack/react-router"; -import { useState } from "react"; +import { createFileRoute, useNavigate } from "@tanstack/react-router"; +import { useEffect, useState } from "react"; import { AuthPageCard } from "@/client/features/auth/AuthPage"; import { bootstrapTeamOwner, @@ -7,27 +7,38 @@ import { } from "@/client/features/auth/teamSetup"; import { authClient } from "@/lib/auth-client"; -// First-run screen for `team` mode: creates the single owner account. It -// redirects to /sign-in the moment an owner exists, so it can't be used to add -// more users. +// First-run screen for `team` mode: creates the single owner account. The +// "already has an owner?" check runs client-side (it needs a DB round trip via +// /api/team-setup, which a server-side beforeLoad can't reach with a relative +// URL) and redirects to /sign-in once an owner exists. export const Route = createFileRoute("/_auth/setup")({ - beforeLoad: async () => { - const status = await fetchTeamSetupStatus(); - if (!status.needsOwner) { - throw redirect({ to: "/sign-in", search: {} }); - } - }, component: SetupPage, }); function SetupPage() { const navigate = useNavigate(); + const [checking, setChecking] = useState(true); const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [isSubmitting, setIsSubmitting] = useState(false); + useEffect(() => { + let cancelled = false; + void fetchTeamSetupStatus().then((status) => { + if (cancelled) return; + if (!status.needsOwner) { + void navigate({ to: "/sign-in", search: {} }); + return; + } + setChecking(false); + }); + return () => { + cancelled = true; + }; + }, [navigate]); + async function handleSubmit(event: React.FormEvent) { event.preventDefault(); setError(null); @@ -57,6 +68,14 @@ function SetupPage() { } } + if (checking) { + return ( +
+ +
+ ); + } + return (