clerk-react-router-patterns

Integrate Clerk authentication into React Router v7 loaders and middleware.

1|Updated Jun 13, 2017
One-click install
npx skills add https://github.com/RoyMcCrain/dotsfile --skill clerk-react-router-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-react-router-patterns
Source: https://github.com/RoyMcCrain/dotsfile/tree/main/claude/skills/clerk-react-router-patterns
Command: npx skills add https://github.com/RoyMcCrain/dotsfile --skill clerk-react-router-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides clear, repeatable patterns for integrating Clerk authentication with React Router v7 so teams can implement secure server- and client-side auth without subtle bugs like missing middleware or infinite redirect loops.

Core Features & Use Cases

  • Middleware + Loader Integration: Shows how to install clerkMiddleware and call rootAuthLoader so getAuth works reliably in nested loaders and actions.
  • Loader and Action Protection: Demonstrates using getAuth inside loaders and actions to perform server-side redirects, 401/403 responses, and scoped data fetching.
  • SSR and Client Hooks: Explains fetching SSR user data via clerkClient/getAuth and consuming reactive client hooks such as useAuth and useUser.
  • Organization Support: Covers org-scoped loaders and the OrganizationSwitcher component for B2B flows.
  • Use Case: Protect a /dashboard route, redirect unauthenticated users to /sign-in, and load org-specific data when orgId is present.

Quick Start

Add clerkMiddleware to your root route, call rootAuthLoader in your root.tsx loader, and use getAuth in any protected loader or action to validate userId and orgId before fetching data.

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 integrate Clerk authentication into React Router v7 loaders?

To integrate Clerk authentication into React Router v7 loaders, you must install clerkMiddleware on your root route and call rootAuthLoader in your root loader. This setup ensures the getAuth function works reliably inside nested loaders and actions for server-side protection.

Why does my Clerk protected route cause an infinite redirect loop in React Router?

Infinite redirect loops in Clerk protected routes usually happen when middleware is missing or redirect handling is misconfigured. Adding clerkMiddleware to the root route and calling rootAuthLoader ensures proper authentication state validation before redirecting unauthenticated users to the sign-in page.

Can I use Clerk organization switching with React Router loaders?

Yes, you can use Clerk organization switching with React Router loaders by implementing org-scoped loaders. You use getAuth inside loaders to validate the orgId and fetch organization-specific data, while the OrganizationSwitcher component handles B2B flows in your client components.

How do I get SSR user data with Clerk in a React Router application?

You get SSR user data with Clerk in a React Router application by using clerkClient and getAuth inside your loaders. This server-side data can then be passed to your components, while reactive client hooks like useAuth and useUser handle client-side authentication state.

Do I need @clerk/react-router to protect routes in React Router v7?

Yes, you need to install @clerk/react-router to protect routes in React Router v7.9+ applications. This package provides the necessary clerkMiddleware, rootAuthLoader, and getAuth functions required to implement secure server-side and client-side authentication flows.

What is the best way to return 401 or 403 responses in React Router actions with Clerk?

The best way to return 401 or 403 responses in React Router actions with Clerk is by using the getAuth function inside your actions. This allows you to validate the userId and orgId on the server before fetching data and returning scoped responses for unauthorized access.