clerk-tanstack-patterns

par clerk

Modèles d'authentification TanStack React Start avec @clerk/tanstack-react-start

npx skills add https://github.com/clerk/skills --skill clerk-tanstack-patterns

TanStack React Start Patterns

What Do You Need?

TaskReference
Protect routes with beforeLoadreferences/router-guards.md
Auth in createServerFnreferences/server-functions.md
Pass auth to loadersreferences/loaders.md
Configure Vinxi + clerkMiddlewarereferences/vinxi-server.md

References

ReferenceDescription
references/router-guards.mdbeforeLoad auth redirect
references/server-functions.mdcreateServerFn with auth()
references/loaders.mdAuth context in loaders
references/vinxi-server.mdclerkMiddleware() setup

Setup

npm install @clerk/tanstack-react-start

.env:

CLERK_PUBLISHABLE_KEY=pk_...
CLERK_SECRET_KEY=sk_...

src/start.ts (Vinxi entry):

import { clerkMiddleware } from '@clerk/tanstack-react-start/server'
import { createStart } from '@tanstack/react-start'

export const startInstance = createStart(() => {
  return {
    requestMiddleware: [clerkMiddleware()],
  }
})

src/routes/__root.tsx — wrap with <ClerkProvider>:

import { ClerkProvider } from '@clerk/tanstack-react-start'

function RootDocument({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ClerkProvider>
          {children}
        </ClerkProvider>
      </body>
    </html>
  )
}

Mental Model

TanStack Start runs on Vinxi. Auth flows through two layers:

  1. Server layer — createServerFn + auth() from @clerk/tanstack-react-start/server
  2. Router layer — beforeLoad on route definitions, throws redirect for unauthenticated

Both layers are server-executed. Client hooks (useAuth, useUser) are React hooks for the browser side.

Minimal Pattern

import { createFileRoute, redirect } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/react-start'
import { auth } from '@clerk/tanstack-react-start/server'

const authStateFn = createServerFn().handler(async () => {
  const { isAuthenticated, userId } = await auth()
  if (!isAuthenticated) {
    throw redirect({ to: '/sign-in' })
  }
  return { userId }
})

export const Route = createFileRoute('/dashboard')({
  beforeLoad: async () => await authStateFn(),
})

Common Pitfalls

SymptomCauseFix
auth() returns emptyMissing clerkMiddleware in start.tsAdd to requestMiddleware array
redirect not thrownUsing return instead of throwthrow redirect(...) in TanStack
Wrong import for authMixing client/server importsServer: @clerk/tanstack-react-start/server
Loader context missing userIdNot passing from beforeLoadReturn from beforeLoad, access via context
ClerkProvider missingForgot root wrappingAdd to __root.tsx shell component

See Also

  • clerk-setup - Initial Clerk install
  • clerk-custom-ui - Custom flows & appearance
  • clerk-orgs - B2B organizations

Docs

TanStack React Start SDK

Plus de skills de clerk

mosaic
clerk
Work on Mosaic UI: styling a component with slot recipes (`defineSlotRecipe` / `useRecipe` / slots / variants), or building a flow — authoring a state machine…
clerk-billing
clerk
Gestion des abonnements Clerk Billing - afficher le PricingTable de Clerk
clerk-nextjs-patterns
clerk
We need to translate the given text from English to French. The text describes an agent skill for Clerk Next.js patterns. We must preserve the name "clerk-nextjs-patterns" but it's not in the text, so we don't include it. We translate only the text inside <text>. No extra labels. Keep technical terms like "Next.js", "Clerk", "auth()", "useAuth()", "unstable_cache", "Server Actions", "Core 2" as is. Translate the rest naturally. Text: "Advanced Next.js patterns for authentication, middleware, Server Actions, and user-scoped caching with Clerk. Distinguishes server-side await auth() from client-side useAuth() hook; mixing them is a common breaking mistake Covers middleware strategies (public-first vs protected-first), API route protection, and proper HTTP status codes (401 vs 403) Includes user-scoped caching patterns with unstable_cache and protecting Server Actions from unauthorized mutations Provides Core 2 compatibility..." Translation: "Modèles avancés Next.js pour l'authentification,
clerk-expo-patterns
clerk
Modèles Expo / React Native avec Clerk — Cache de jetons SecureStore, OAuth
changesets
clerk
Create or refresh a `.changeset/<slug>.md` for the current branch, or report that none is required. Triggers on "/changesets create", "add a changeset",…
clerk
clerk
Le binaire clerk est une passerelle pré-authentifiée vers l'API Backend et l'API Platform de Clerk, ainsi que des outils au niveau du projet (authentification, liaison, récupérations d'environnement, configuration d'instance). Lorsque l'utilisateur demande quoi que ce soit qui touche à une ressource Clerk, utilisez clerk en premier lieu plutôt que de bricoler une commande curl.
clerk-cli
clerk
Le binaire clerk est une passerelle pré-authentifiée vers l'API Backend et l'API Platform de Clerk, ainsi que des outils au niveau du projet (auth, linking, env pulls, configuration d'instance). Lorsque l'utilisateur demande quoi que ce soit qui touche à une ressource Clerk, utilisez clerk en premier plutôt que de faire un curl à la main.
clerk-astro-patterns
clerk
Modèles Astro avec Clerk — middleware, pages SSR, composants îlots,