clerk-astro-patterns

von clerk

Astro-Muster mit Clerk — Middleware, SSR-Seiten, Inselkomponenten,

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

Astro Patterns

SDK: @clerk/astro v4+. Requires Astro 5+.

What Do You Need?

TaskReference
Configure middleware (bare clerkMiddleware(), checks per page)references/middleware.md
Protect SSR pagesreferences/ssr-pages.md
Use Clerk in island componentsreferences/island-components.md
Auth in API routesreferences/api-routes.md
Use Clerk with React in Astroreferences/astro-react.md

Mental Model

Astro has two rendering modes per page: SSR and static prerender. Clerk works differently in each:

  • SSR pages — use Astro.locals.auth() which is populated by the middleware
  • Static pages (export const prerender = true) — Clerk middleware skips them; use client-side hooks in islands
  • Islands — React/Vue/Svelte components; use useAuth() and other hooks from @clerk/astro/react
Request → clerkMiddleware() → SSR page → Astro.locals.auth()
                                ↓
                         Island (.client) → useAuth() hook

Setup

astro.config.mjs

import { defineConfig } from 'astro/config'
import clerk from '@clerk/astro'

export default defineConfig({
  integrations: [clerk()],
  output: 'server',
})

src/middleware.ts

import { clerkMiddleware } from '@clerk/astro/server'

export const onRequest = clerkMiddleware()

The middleware populates Astro.locals.auth(). Each page and API route checks for itself. createRouteMatcher was removed in @clerk/astro 4.0 (deprecated in 3.x). Don't add it. Existing uses must move to page and API-route checks before upgrading. See the Astro clerkMiddleware() reference.

SSR Page Auth

---
const { userId, orgId } = Astro.locals.auth()
if (!userId) return Astro.redirect('/sign-in')
---

<h1>Dashboard</h1>

API Route Auth

// src/pages/api/data.ts
import type { APIRoute } from 'astro'

export const GET: APIRoute = ({ locals }) => {
  const { userId } = locals.auth()
  if (!userId) return new Response('Unauthorized', { status: 401 })

  return Response.json({ userId })
}

Common Pitfalls

SymptomCauseFix
Astro.locals.auth is undefinedMissing middlewareAdd clerkMiddleware to src/middleware.ts
Auth works in dev but not productionoutput: 'static' globallySet output: 'server' or hybrid for protected pages
Static page has no authPrerendered pages skip middlewareUse export const prerender = false or move to island
Island not reactive to sign-inMissing client:load directiveAdd client:load to the island component

Import Map

WhatImport From
clerkMiddleware, clerkClient@clerk/astro/server
useAuth, UserButton (no useUser)@clerk/astro/react
$userStore, $authStore@clerk/astro/client
Astro components (<SignIn>, etc.)@clerk/astro/components

Env Variables

# .env
PUBLIC_CLERK_PUBLISHABLE_KEY=pk_...
CLERK_SECRET_KEY=sk_...

Astro uses PUBLIC_ prefix for client-exposed variables (not NEXT_PUBLIC_).

See Also

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

Docs

Astro SDK

Mehr Skills von clerk

clerk-testing
clerk
E2E-Testhilfsprogramme für Clerk-Authentifizierungsabläufe in Playwright und Cypress. Unterstützt sowohl Playwright- als auch Cypress-Frameworks mit frameworkspezifischen Einrichtungsmustern (globalSetup für Playwright, benutzerdefinierte Befehle für Cypress). Bietet die Hilfsprogramme clerkSetup() und setupClerkTestingToken() zur Initialisierung von Testumgebungen und Umgehung der Bot-Erkennung. Enthält Speicherstatus-Persistenz zur Wiederverwendung authentifizierter Sitzungen über Tests hinweg, wodurch die Testausführungszeit reduziert wird. Erfordert Test-API-Schlüssel ( pk_test_* ,...
clerk-swift
clerk
We need to translate the given English text into German, preserving the name "clerk-swift" if it appears, but it doesn't appear in the text. The text is a description of an agent skill. We must not add any extra commentary, labels, or formatting. Just the translation. The text: "Native Swift/iOS authentication using ClerkKit with prebuilt or custom auth flows. Supports two implementation modes: prebuilt AuthView components or fully custom native flows, selected based on project requirements Requires direct wiring of a valid Clerk publishable key in app configuration; does not use plist or environment file indirection by default Mandatory /v1/environment call after package install to determine feature availability (for example Apple Sign In support) based on..." We need to translate this into German. Note: "ClerkKit", "AuthView", "Apple Sign In", "/v1/environment" are technical terms/protocols/names, so preserve them. Also "plist" is a technical term. "publishable key" is a term. "Swift
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
Clerk Billing für Abonnementverwaltung – rendert Clerk's PricingTable
clerk-nextjs-patterns
clerk
Fortgeschrittene Next.js-Muster für Authentifizierung, Middleware, Server Actions und benutzerspezifisches Caching mit Clerk. Unterscheidet serverseitiges await auth() vom clientseitigen useAuth()-Hook; deren Vermischung ist ein häufiger, schwerwiegender Fehler. Behandelt Middleware-Strategien (öffentlich-zuerst vs. geschützt-zuerst), API-Routenschutz und korrekte HTTP-Statuscodes (401 vs. 403). Enthält benutzerspezifische Caching-Muster mit unstable_cache und den Schutz von Server Actions vor unbefugten Mutationen. Bietet Core 2-Kompatibilität...
clerk-expo-patterns
clerk
Expo / React Native Patterns mit Clerk — SecureStore-Token-Cache, 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
Die clerk-Binärdatei ist ein vorauthentifiziertes Gateway zur Clerk Backend API und Platform API, plus projektbezogene Werkzeuge (Authentifizierung, Verknüpfung, Umgebungsabrufe, Instanzkonfiguration). Wenn der Benutzer etwas in Bezug auf eine Clerk-Ressource fragt, greifen Sie zuerst zu clerk, anstatt curl manuell zu verwenden.