Setting the file. One moment.
Chapter 17 · Clerk React Router Patterns
Subchapter 17.1
references/loaders-actions.mdMarkdown2 KBView on GitHub
Also bundled
Evalsimport { getAuth } from '@clerk/react-router/server'
import { redirect } from 'react-router'
import type { Route } from './+types/dashboard'
export async function loader(args: Route.LoaderArgs) {
const { userId, orgId, sessionId } = await getAuth(args)
if (!userId) throw redirect('/sign-in')
return { data: await db.query(userId) }
}export async function action(args: Route.ActionArgs) {
const { userId } = await getAuth(args)
if (!userId) throw new Response('Unauthorized', { status: 401 })
const fd = await args.request.formData()
await db.insert({ userId, title: fd.get('title') })
return redirect('/dashboard')
}Pass a callback to rootAuthLoader when you need auth state in the root route:
import { rootAuthLoader } from '@clerk/react-router/server'
export async function loader(args: Route.LoaderArgs) {
return rootAuthLoader(args, async ({ request }) => {
const { userId } = request.auth
if (!userId) return { user: null }
return { user: await db.getUser(userId) }
})
}| Field | Type | Description |
|---|---|---|
userId | string | null | Current user ID |
sessionId | string | null | Current session ID |
orgId | string | null | Active org ID |
orgRole | string | null | User’s role in active org |
has() | function | Check permissions/features |
getAuth throws if clerkMiddleware is not installedrootAuthLoader must be called in root.tsx loader — not just any loaderredirect from react-router, not from @remix-run/react-router