clerk-react-router-patterns

Wire Clerk authentication into React Router v7 loaders and middleware.

Updated May 4, 2026
One-click install
npx skills add https://github.com/Endsi3g/Uprising-AOS --skill clerk-react-router-patterns-endsi3g
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-react-router-patterns
Source: https://github.com/Endsi3g/Uprising-AOS/tree/main/.agents/skills/clerk-react-router-patterns
Command: npx skills add https://github.com/Endsi3g/Uprising-AOS --skill clerk-react-router-patterns-endsi3g

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This guide prevents broken authentication flows in React Router v7 apps by showing the correct integration points for Clerk so loaders and nested routes reliably enforce access control.

Core Features & Use Cases

  • Root-level auth state wiring: Uses rootAuthLoader in root.tsx so getAuth works in nested loaders without throwing.
  • Loader and action protection: Applies getAuth in server loaders/actions to gate routes and handle unauthenticated requests with redirects or 401s.
  • Client user context and org switching: Uses useAuth/useUser for client navigation data and OrganizationSwitcher for org-scoped UX, including redirects when orgId is missing.

Quick Start

Set up rootAuthLoader(args) in your root.tsx loader, export middleware = [clerkMiddleware()], then use getAuth(args) inside your protected route loaders to redirect unauthenticated users to /sign-in.

Frequently Asked Questions about clerk-react-router-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up Clerk authentication in React Router v7 loaders?

To set up protected routes with Clerk in React Router, call `getAuth(args)` inside your server loaders and check for a valid `userId`. If the user is unauthenticated, redirect them to the sign-in page or return a 401 error to block access.

Why does getAuth throw errors in my nested React Router loaders?

The `getAuth` function throws errors in nested loaders when root auth state is missing. You must wire `rootAuthLoader` in your `root.tsx` file to establish the necessary authentication context for downstream nested routes to function correctly.

How do I handle organization switching and missing orgId in React Router?

To handle organization switching, use the `OrganizationSwitcher` component for org-scoped UX and `useAuth` for client navigation data. In nested loaders, check for a missing `orgId` and redirect users to maintain proper access control.

Can I use Clerk middleware to protect server-side data loading in React Router?

Yes, you can use `clerkMiddleware` to protect server-side data loading in React Router. By combining it with `getAuth` in nested loaders and actions, you can securely gate routes and handle unauthenticated requests with redirects or 401 errors.

What is the best way to gate authenticated actions in React Router v7?

The best way to gate authenticated actions in React Router v7 is using `getAuth(args)` within your action functions. Verify the presence of `userId` or `orgId` and respond with appropriate errors or redirects when authentication or organization authorization is missing.