MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / AuthGate

Function AuthGate

apps/cloud/src/routes/__root.tsx:190–332  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

188}
189
190function AuthGate() {
191 const auth = useAuth();
192 const location = useLocation();
193 const navigate = useNavigate();
194 const isOnboardingRoute = ONBOARDING_PATHS.has(location.pathname);
195 const isPublicRoute = PUBLIC_PATHS.has(location.pathname);
196 // The org the URL names (the `{-$orgSlug}` segment), if any. `/account/me`
197 // is scoped to it, so `auth.organization` IS this org when the caller is a
198 // member — and `null` when the URL names an org they can't access.
199 const urlOrgSlug = (useParams({ strict: false }) as { orgSlug?: string }).orgSlug;
200 // The same slug derived from the PATHNAME instead of the route params: the
201 // params resolve asynchronously (a fresh load renders once with no orgSlug,
202 // then again with it), and anything keyed on them remounts on that flap.
203 // The pathname is synchronously correct on the very first render, and it is
204 // exactly what the request header derives from (getActiveOrgSlug), so the
205 // registry scope below can never disagree with the header scope.
206 const firstSegment = location.pathname.split("/")[1] ?? "";
207 const pathnameOrgSlug = isValidOrgSlug(firstSegment) ? firstSegment : null;
208
209 // The SSR gate already bounced fresh org-less document requests to
210 // /create-org; this catches the MID-SESSION transitions (org deleted,
211 // membership revoked → /account/me now reports no org). Only for BARE paths:
212 // an org-less result on a slugged URL is a wrong address (404 below), not a
213 // reason to send the user to onboarding.
214 const needsOrgRedirect =
215 auth.status === "authenticated" &&
216 auth.organization == null &&
217 !urlOrgSlug &&
218 !isOnboardingRoute &&
219 !isPublicRoute;
220
221 React.useEffect(() => {
222 if (needsOrgRedirect) {
223 void navigate({ to: "/create-org", replace: true });
224 }
225 }, [needsOrgRedirect, navigate]);
226
227 // The signed-out safety net behind the SSR gate: if a session dies while
228 // the SPA is already loaded (logout elsewhere, expiry), go to /login the
229 // same way a fresh document request would — keeping where they were.
230 const needsLoginRedirect = auth.status === "unauthenticated" && !isPublicRoute;
231 React.useEffect(() => {
232 if (needsLoginRedirect) {
233 window.location.assign(loginPath(`${location.pathname}${location.searchStr}`));
234 }
235 }, [needsLoginRedirect, location.pathname, location.searchStr]);
236
237 if (isPublicRoute) {
238 return <Outlet />;
239 }
240
241 // Every state that isn't "authenticated with an org, on a page that wants
242 // the shell" is a moment between redirects, or the one frame between mount
243 // and the hint cookie seeding (SPA mode reads it in an effect). Neutral
244 // blank — the one placeholder that's correct whatever happens next. The
245 // app-shell skeleton this file used to render here is exactly the
246 // wrong-UI flash the document gate + hint exist to prevent.
247 if (auth.status === "loading" || auth.status === "unauthenticated") {

Callers

nothing calls this directly

Calls 3

useAuthFunction · 0.90
isValidOrgSlugFunction · 0.90
loginPathFunction · 0.90

Tested by

no test coverage detected