clerk-react-router-patterns

Configure Clerk authentication patterns for React Router v7 loaders and middleware.

1|Updated Apr 11, 2026
One-click install
npx skills add https://github.com/RajAryanIITBHU/bny --skill clerk-react-router-patterns-rajaryaniitbhu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-react-router-patterns
Source: https://github.com/RajAryanIITBHU/bny/tree/main/.continue/skills/clerk-react-router-patterns
Command: npx skills add https://github.com/RajAryanIITBHU/bny --skill clerk-react-router-patterns-rajaryaniitbhu

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides clear, actionable patterns for integrating Clerk authentication into React Router v7 applications so that server-side loaders, middleware, and client components can reliably detect and enforce user identity and organization context. It prevents common runtime failures like getAuth throwing when middleware or root loader setup is missing and reduces redirect loops and incorrect access checks.

Core Features & Use Cases

  • Middleware integration guidance to attach auth into the request pipeline and ensure getAuth works in nested loaders.
  • rootAuthLoader usage for passing Clerk state from the server root loader to the client and an optional callback for loading user data on the server.
  • Usage patterns for getAuth in loaders and actions to enforce per-route authentication, perform org-scoped redirects, and obtain tokens for external APIs.
  • SSR user data examples and OrganizationSwitcher usage for org-scoped flows and admin gating.
  • Practical troubleshooting notes covering missing middleware, infinite redirects, and correct redirect semantics in loaders and actions.

Quick Start

Add clerkMiddleware at the root, call rootAuthLoader in your root loader, and use getAuth in protected loaders and actions 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 Clerk authentication in React Router v7 loaders, export clerkMiddleware at the root, call rootAuthLoader from your root loader, and use getAuth inside nested loaders to enforce per-route access.

Why does getAuth throw an error in my React Router loader?

getAuth throws an error in React Router loaders when clerkMiddleware is missing at the root or rootAuthLoader is not called in the root loader, breaking the server-side authentication pipeline.

How do I load Clerk user data during SSR in React Router?

To load Clerk user data during SSR in React Router, use rootAuthLoader in your root loader with its optional callback to fetch user data on the server and pass the state to the client.

Can I use Clerk organization switching with React Router v7?

Yes, you can use Clerk organization switching with React Router v7 by implementing OrganizationSwitcher and using getAuth in loaders to perform org-scoped redirects and gate admin access.

How do I prevent infinite redirect loops with Clerk auth in React Router?

To prevent infinite redirect loops with Clerk auth in React Router, ensure correct redirect semantics in loaders and actions by checking authentication state before redirecting unauthenticated users to /sign-in.

Do I need Clerk middleware to protect React Router actions?

Yes, you need clerkMiddleware exported at the root to protect React Router actions, as it attaches auth into the request pipeline ensuring getAuth works correctly in both loaders and actions.