clerk-tanstack-patterns

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

Updated Apr 19, 2026
One-click install
npx skills add https://github.com/divinaarmuela/Content --skill clerk-tanstack-patterns-divinaarmuela
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-tanstack-patterns
Source: https://github.com/divinaarmuela/Content/tree/main/.claude/skills/clerk-tanstack-patterns
Command: npx skills add https://github.com/divinaarmuela/Content --skill clerk-tanstack-patterns-divinaarmuela

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 into two server layers (Vinxi middleware and createServerFn) plus router-level guards, and small mistakes like wrong import paths or missing middleware cause silent auth failures. ## Core Features & Use Cases - Route Protection: Guard routes with beforeLoad checks that call auth() via createServerFn and throw redirects for unauthenticated users, including layout-route guards that protect entire route groups. - Server Functions & Loaders: Access userId and orgId inside createServerFn handlers and pass auth context from beforeLoad into loaders for org-scoped data fetching. - Vinxi Server Setup: Register clerkMiddleware in start.ts and wrap the root route with ClerkProvider so auth() works across server functions and API routes. - Use Case: You need to protect a /dashboard route so unauthenticated visitors are redirected to /sign-in, then load that user's projects in the route loader using the userId passed through route context. ## Quick Start Ask the AI to protect a TanStack Start route with Clerk by adding a beforeLoad auth guard 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 server function in the route's beforeLoad. Child routes of a guarded layout route inherit the protection automatically.

How do I use auth() inside createServerFn in TanStack Start?

Import auth from @clerk/tanstack-react-start/server inside your createServerFn handler and await it to get isAuthenticated, userId, and orgId. Throw an error or redirect when the user is not authenticated, then return the auth data for loaders or components.

Why does auth() return an empty object in my TanStack Start app?

This happens when clerkMiddleware is not registered in src/start.ts. Add clerkMiddleware() to the requestMiddleware array passed to createStart so the Clerk context is available to all server functions.

What is the difference between the server and client imports in @clerk/tanstack-react-start?

Server-side auth() and clerkMiddleware come from the @clerk/tanstack-react-start/server subpath, while client hooks like useAuth and useUser come from the package root. Mixing these import paths causes runtime errors.

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

Return the userId from beforeLoad and access it in the loader through the context argument. The loader can then query your database scoped to that userId, and the component reads results via Route.useLoaderData().

How do I protect a TanStack Start API route with Clerk?

Call auth() from @clerk/tanstack-react-start/server inside the API route handler and return a 401 Response when isAuthenticated is false. Extract userId or sessionId from the auth result for authorized requests.