clerk-astro-patterns

Implements Clerk authentication patterns for Astro middleware, SSR pages, islands, and API routes.

Updated Sep 19, 2026
One-click install
npx skills add https://github.com/paramcodes/autobro --skill clerk-astro-patterns-paramcodes
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: clerk-astro-patterns
Source: https://github.com/paramcodes/autobro/tree/main/.agents/skills/clerk-astro-patterns
Command: npx skills add https://github.com/paramcodes/autobro --skill clerk-astro-patterns-paramcodes

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @clerk/astro, astro, @astrojs/node, @astrojs/react, and includes references (resource) components.

What problem does it solve? Integrating Clerk authentication into Astro apps is tricky because Astro has two rendering modes (SSR and static prerender) plus island components, and Clerk behaves differently in each. This Skill provides correct, tested patterns so you avoid common pitfalls like undefined auth objects, skipped middleware, and non-reactive islands. ## Core Features & Use Cases - Middleware Configuration: Set up clerkMiddleware with createRouteMatcher to protect routes like /dashboard and redirect unauthenticated users to sign-in. - SSR & API Route Auth: Access Astro.locals.auth() in server-rendered pages and API endpoints, with proper 401/403 status codes and org-scoped checks. - Island Components: Use useAuth, UserButton, and SignInButton from @clerk/astro/react in hydrated React islands with the correct client directives. - Use Case: You are building an Astro dashboard with mixed static and dynamic pages. Use this Skill to configure middleware, protect SSR pages, and add a client-side header that shows a sign-in button or user menu based on auth state. ## Quick Start Set up Clerk middleware in my Astro app to protect the /dashboard route and redirect unauthenticated users to /sign-in.

Frequently Asked Questions about clerk-astro-patterns

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

FAQPage Schema
How do I protect routes with Clerk middleware in Astro?▼

Create src/middleware.ts exporting onRequest from clerkMiddleware, and use createRouteMatcher to match protected paths like /dashboard(.*). Inside the handler, call auth().redirectToSignIn() when the route matches and auth().userId is falsy, otherwise return next().

How to get the current user in an Astro SSR page with Clerk?▼

Call Astro.locals.auth() in the page frontmatter to destructure userId, orgId, and other fields. Redirect with Astro.redirect('/sign-in') when userId is missing. The page must not have export const prerender = true for server auth to work.

Why is Astro.locals.auth undefined in my Astro app?▼

Astro.locals.auth is undefined when clerkMiddleware is missing from src/middleware.ts, since the middleware is what populates the auth object. It also fails on statically prerendered pages because middleware is skipped for pages with export const prerender = true.

Does Clerk work on statically prerendered Astro pages?▼

No, Clerk middleware skips pages with export const prerender = true, so Astro.locals.auth() is unavailable there. Either set export const prerender = false to make the page SSR, or use client-side hooks like useAuth inside a hydrated island component.

How do I use Clerk React components in Astro islands?▼

Import components like UserButton, SignInButton, and Show from @clerk/astro/react, not @clerk/astro/components. Mount the component in your .astro file with a client:load directive, since without hydration Clerk hooks return undefined and the UI is not reactive.

How to return 401 from an Astro API route with Clerk?▼

In your API route handler, call context.locals.auth() to get userId and return new Response('Unauthorized', { status: 401 }) when it is falsy. Use 403 for authenticated users lacking permissions, checked via auth.has({ permission: '...' }).