clerk-astro-patterns

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

Updated Aug 22, 2026
One-click install
npx skills add https://github.com/Rajaryan1726/ai-code-review-system --skill clerk-astro-patterns-rajaryan1726
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: clerk-astro-patterns
Source: https://github.com/Rajaryan1726/ai-code-review-system/tree/main/client/.agents/skills/clerk-astro-patterns
Command: npx skills add https://github.com/Rajaryan1726/ai-code-review-system --skill clerk-astro-patterns-rajaryan1726

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 the correct patterns for middleware setup, SSR page auth, island components, and API route protection so you avoid common pitfalls like undefined Astro.locals.auth or 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: Use Astro.locals.auth() in SSR pages and context.locals.auth() in API routes, with proper 401/403 status codes and org/permission checks. - Island Components: Use useAuth, UserButton, and SignInButton from @clerk/astro/react with the required client:load hydration directive. - Use Case: You are adding Clerk to an Astro app with mixed static and dynamic pages and need to protect /dashboard, build an auth-aware header island, and secure an API endpoint — this Skill gives you the exact code patterns and rendering-mode guidance for each. ## 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 missing, otherwise return next().

How do I 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 falsy, and ensure the page is not marked with export const prerender = true.

Does Clerk middleware work on statically prerendered Astro pages?▼

No, Clerk middleware is skipped for pages with export const prerender = true. Either set export const prerender = false to make the page SSR, or use client-side hooks like useAuth inside an island component for static pages.

Why is my Clerk React island not reacting to sign-in state in Astro?▼

The island is missing a client directive, so it renders as static HTML and Clerk hooks return undefined. Add client:load to the component in the .astro file and import hooks from @clerk/astro/react, not @clerk/react.

How do I protect an Astro API route with Clerk authentication?▼

In the API route handler, call context.locals.auth() to get userId and return new Response('Unauthorized', { status: 401 }) when it is missing. API routes are always SSR, so prerender settings do not apply, and you can also check orgId or permissions for 403 responses.