Skip to main content
skill CX Codex

clerk-astro-patterns — Claude Code skill for Clerk in Astro

Walks your agent through Clerk inside Astro: middleware, protected SSR pages, API route auth and island components.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add clerk/skills --skill clerk-astro-patterns -a claude-code
Open on GitHub

What clerk-astro-patterns does

The skill covers how Clerk auth behaves across Astro's two rendering modes. SSR pages read Astro.locals.auth(), filled in by the middleware. Static pages with export const prerender = true skip that middleware entirely, so auth there lives in client-side hooks inside islands.

Inside are task-based references: middleware setup, SSR page protection, Clerk in island components, auth in API routes, and React in Astro. A pitfalls table maps symptoms to causes. Astro.locals.auth comes back undefined when src/middleware.ts has no clerkMiddleware; an island that ignores sign-in usually just lacks the client:load directive.

You need @clerk/astro v4+ and Astro 5+. Two env vars go in .env: PUBLIC_CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY. Astro exposes client variables with the PUBLIC_ prefix, not NEXT_PUBLIC_. If your codebase still calls createRouteMatcher, that is worth checking before you upgrade: the function was removed in @clerk/astro 4.0, and those checks have to move into pages and API routes.

What it helps with

  • protect an SSR page and redirect signed-out visitors
  • check the user inside an API route and return 401
  • wire Clerk into a React island component
  • debug auth that works in dev but fails in production
  • migrate off createRouteMatcher to per-page checks

Example prompts for the agent

  • Set up clerkMiddleware in src/middleware.ts and show me how to protect an SSR page
  • Add a userId check to src/pages/api/data.ts and return 401 without a session
  • Why does my island ignore sign-in? Check the client:load directive

How to install clerk-astro-patterns

1Skills CLI (Vercel)

npx skills add clerk/skills --skill clerk-astro-patterns -a claude-code

2Manually: put the files in place

~/.claude/skills/clerk-astro-patterns/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/clerk-astro-patterns/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about clerk-astro-patterns

How do I install clerk-astro-patterns in Claude?

Run npx skills add clerk/skills --skill clerk-astro-patterns -a claude-code. The skill lands in ~/.claude/skills/clerk-astro-patterns/SKILL.md.

What does clerk-astro-patterns do?

It gives your agent ready patterns for Clerk in Astro: middleware, SSR page protection, API route auth and hooks in island components.

Which versions does the skill require?

@clerk/astro v4+ and Astro 5+. Since createRouteMatcher was removed in @clerk/astro 4.0, older checks need to move into pages and API routes.

Source text

SKILL.md · MIT
---
name: clerk-astro-patterns
description: 'Astro patterns with Clerk — middleware, SSR pages, island components,
  API routes, static vs SSR rendering. Triggers on: astro clerk, clerk astro middleware,
  astro protected page, clerk island component, astro API route auth, clerk astro
  SSR.'
license: MIT
allowed-tools: WebFetch
metadata:
  author: clerk
  version: 1.0.0
---

# Astro Patterns

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

## What Do You Need?

| Task | Reference |
|------|-----------|
| Configure middleware (bare `clerkMiddleware()`, checks per page) | references/middleware.md |
| Protect SSR pages | references/ssr-pages.md |
| Use Clerk in island components | references/island-components.md |
| Auth in API routes | references/api-routes.md |
| Use Clerk with React in Astro | references/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

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

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

### src/middleware.ts

```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](https://clerk.com/docs/reference/astro/clerk-middleware).

## SSR Page Auth

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

<h1>Dashboard</h1>
```

## API Route Auth

```ts
// 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

| Symptom | Cause | Fix |
|---------|-------|-----|
| `Astro.locals.auth` is undefined | Missing middleware | Add `clerkMiddleware` to `src/middleware.ts` |
| Auth works in dev but not production | `output: 'static'` globally | Set `output: 'server'` or `hybrid` for protected pages |
| Static page has no auth | Prerendered pages skip middleware | Use `export const prerender = false` or move to island |
| Island not reactive to sign-in | Missing `client:load` directive | Add `client:load` to the island component |

## Import Map

| What | Import 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](https://clerk.com/docs/astro/getting-started/quickstart)

© clerk · MIT · clerk/skills

Similar skills

Everything for Frontend and UI →

From the same set19

clerk-react-router-patterns Guides Clerk integration with React Router v7/v8, including loaders, protected routes, SSR auth, and organization switching. Useful when wiring auth into a router. CX Codex +1 15,500installs clerk-tanstack-patterns Walks your agent through Clerk auth in TanStack React Start: route guards, createServerFn, loaders and request middleware. CX Codex +1 15,500installs clerk-swift Builds Clerk sign-in UI in native iOS and macOS apps: prebuilt ClerkKitUI views or custom ClerkKit flows. CX Codex +1 14,500installs clerk-chrome-extension-patterns Guides integration of @clerk/chrome-extension into Chrome extensions. Helps configure popup sign-in, service worker session tokens, and OAuth delegation via syncHost. CX Codex +1 14,400installs clerk-android A skill that walks your agent through adding Clerk to a native Android app in Kotlin and Jetpack Compose, using prebuilt AuthView/UserButton or a custom API flow. CX Codex +1 14,400installs clerk-expo A skill that wires Clerk sign-in and sign-up into Expo and React Native apps. Useful when you want prebuilt auth screens or custom flows with OTP and MFA. CX Codex +1 14,100installs clerk-vue-patterns A skill that tells your agent how to wire Vue 3 to Clerk: composables, Vue Router guards and a Pinia auth store. CX Codex +1 14,000installs clerk-nuxt-patterns Provides @clerk/nuxt authentication patterns for Nuxt 3 projects. It helps configure Nitro server routes, client composables, and SSR route guards. CX Codex +1 13,900installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor