Setting the file. One moment.
Chapter 18 · Clerk TanStack Patterns
Subchapter 18.2
references/router-guards.mdMarkdown2 KBView on GitHub
Also bundled
EvalsbeforeLoad runs before the route renders. Throw a redirect to block unauthenticated access:
import { createFileRoute, redirect } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/react-start'
import { auth } from '@clerk/tanstack-react-start/server'
const checkAuth = createServerFn().handler(async () => {
const { isAuthenticated, userId } = await auth()
if (!isAuthenticated) {
throw redirect({ to: '/sign-in' })
}
return { userId }
})
export const Route = createFileRoute('/dashboard')({
beforeLoad: async () => await checkAuth(),
})export const Route = createFileRoute('/dashboard')({
beforeLoad: async () => {
const { userId } = await checkAuth()
return { userId }
},
loader: async ({ context }) => {
const { userId } = context
const data = await fetchUserData(userId)
return { data }
},
component: Dashboard,
})
function Dashboard() {
const { data } = Route.useLoaderData()
return <div>{JSON.stringify(data)}</div>
}Protect a group of routes with a single layout check:
// src/routes/_authenticated.tsx
import { createFileRoute, redirect, Outlet } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/react-start'
import { auth } from '@clerk/tanstack-react-start/server'
const getAuth = createServerFn().handler(async () => {
const { isAuthenticated, userId } = await auth()
if (!isAuthenticated) {
throw redirect({ to: '/sign-in' })
}
return { userId }
})
export const Route = createFileRoute('/_authenticated')({
beforeLoad: async () => await getAuth(),
component: () => <Outlet />,
})Child routes under /_authenticated/ inherit the guard automatically.
throw redirect({
to: '/sign-in',
search: { redirect: window.location.pathname },
})