clerk-tanstack-patterns

Implements Clerk authentication patterns for TanStack React Start routes, loaders, and server functions.

Updated Aug 22, 2026
One-click install
npx skills add https://github.com/Rajaryan1726/ai-code-review-system --skill clerk-tanstack-patterns-rajaryan1726
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: clerk-tanstack-patterns
Source: https://github.com/Rajaryan1726/ai-code-review-system/tree/main/client/.agents/skills/clerk-tanstack-patterns
Command: npx skills add https://github.com/Rajaryan1726/ai-code-review-system --skill clerk-tanstack-patterns-rajaryan1726

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @clerk/tanstack-react-start, @tanstack/react-router, @tanstack/react-start, and includes references (resource) components.

What problem does it solve? Adding authentication to a TanStack React Start app requires wiring Clerk through two server-executed layers (createServerFn and beforeLoad), and common mistakes like missing clerkMiddleware, wrong import paths, or returning instead of throwing redirects cause silent auth failures. ## Core Features & Use Cases - Route Protection: Guard routes with beforeLoad checks that call auth() inside createServerFn and throw redirects for unauthenticated users, including layout-route guards that protect entire route groups. - Server Functions & Loaders: Pass userId and orgId through route context into loaders, build org-scoped data fetching, and protect API routes with 401 responses. - Vinxi Setup: Configure clerkMiddleware in start.ts and ClerkProvider in the root route so auth() works across server functions. - Use Case: You need to protect a /dashboard route so unauthenticated visitors are redirected to /sign-in; the Skill provides the exact createServerFn + beforeLoad pattern with correct imports. ## Quick Start Ask the AI to protect a TanStack Start route with Clerk authentication using beforeLoad and a server function that redirects unauthenticated users to the sign-in page.

Frequently Asked Questions about clerk-tanstack-patterns

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

FAQPage Schema
How do I protect a TanStack Start route with Clerk authentication?▼

Create a createServerFn that calls auth() from @clerk/tanstack-react-start/server, throw redirect({ to: '/sign-in' }) when isAuthenticated is false, and call that function in the route's beforeLoad. The redirect must be thrown, not returned.

How do I pass userId from beforeLoad to a TanStack loader?▼

Return the userId from beforeLoad and access it in the loader via the context parameter. The loader can then use context.userId to fetch user-scoped data, and the component reads results with Route.useLoaderData().

Why does auth() return an empty object in my TanStack Start server function?▼

The clerkMiddleware is missing from your Vinxi server setup. Add clerkMiddleware() to the requestMiddleware array in src/start.ts so the auth context is populated for all server functions.

What is the correct import path for auth() in TanStack React Start?▼

Import auth from @clerk/tanstack-react-start/server for server-side code. Client hooks like useAuth and useUser come from the package root; mixing these import paths causes runtime errors.

Can I protect a group of routes with a single auth check in TanStack Router?▼

Yes, create a layout route such as _authenticated.tsx with a beforeLoad auth check and an Outlet component. All child routes under that layout inherit the guard without repeating the check.

How do I handle users with no active organization in a route loader?▼

Read orgId from auth() in a server function, and in the loader return an empty state with a requiresOrg flag when orgId is null. The component can then render an OrganizationSwitcher or a prompt to select an organization.