diff --git a/web/app/router.tsx b/web/app/router.tsx index f6ff9595..959bf8b5 100644 --- a/web/app/router.tsx +++ b/web/app/router.tsx @@ -13,7 +13,7 @@ export function createRouter() { defaultPreload: "intent", defaultErrorComponent: DefaultCatchBoundary, defaultNotFoundComponent: () => , - context: { queryClient }, + context: { queryClient, auth: undefined! }, }), queryClient, ) diff --git a/web/app/routes/__root.tsx b/web/app/routes/__root.tsx index 65f27489..fa07330c 100644 --- a/web/app/routes/__root.tsx +++ b/web/app/routes/__root.tsx @@ -44,6 +44,7 @@ export const fetchClerkAuth = createServerFn("GET", async (_, ctx) => { }) export const Route = createRootRouteWithContext<{ + auth: { userId: string } queryClient: QueryClient }>()({ meta: () => [ @@ -77,15 +78,13 @@ export const Route = createRootRouteWithContext<{ { rel: "manifest", href: "/site.webmanifest", color: "#fffff" }, { rel: "icon", href: "/favicon.ico" }, ], - beforeLoad: async ({ cause }) => { - if (cause !== "stay") { - const auth = await fetchClerkAuth() - return { auth } + beforeLoad: async ({ context }) => { + if (context.auth) { + return { auth: context.auth } } - return { - auth: null, - } + const auth = await fetchClerkAuth() + return { auth } }, errorComponent: (props) => { return ( diff --git a/web/app/routes/_layout/_pages/_protected.tsx b/web/app/routes/_layout/_pages/_protected.tsx index fd0f7134..8e263cbe 100644 --- a/web/app/routes/_layout/_pages/_protected.tsx +++ b/web/app/routes/_layout/_pages/_protected.tsx @@ -1,8 +1,7 @@ import { createFileRoute, Outlet, redirect } from "@tanstack/react-router" export const Route = createFileRoute("/_layout/_pages/_protected")({ - beforeLoad: ({ context, location, cause }) => { - if (cause === "stay") return + beforeLoad: async ({ context, location, cause }) => { if (!context?.auth?.userId) { throw redirect({ to: "/sign-in/$",