clerk-astro-patterns

Secure Astro SSR routes and API endpoints with Clerk middleware.

Updated May 4, 2026
One-click install
npx skills add https://github.com/Endsi3g/Uprising-AOS --skill clerk-astro-patterns-endsi3g
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clerk-astro-patterns
Source: https://github.com/Endsi3g/Uprising-AOS/tree/main/.agents/skills/clerk-astro-patterns
Command: npx skills add https://github.com/Endsi3g/Uprising-AOS --skill clerk-astro-patterns-endsi3g

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill solves the common confusion of securing Astro apps with Clerk when pages can be SSR, static prerendered, or client-hydrated islands.

Core Features & Use Cases

  • Middleware protection: Use clerkMiddleware and createRouteMatcher to guard SSR routes like dashboards and settings.
  • SSR auth inside pages: Read authenticated context via Astro.locals.auth() and redirect unauthenticated users.
  • Client-only islands: Use React Clerk hooks (e.g., useAuth, useUser) in client:load islands so auth works for static/prerendered pages.
  • Authenticated API routes: Validate access inside API routes using context.locals.auth() and return correct HTTP status codes.
  • Org-aware rendering: Handle both missing user sessions and missing active organization with redirects.

Quick Start

Tell your AI to “Set up Clerk for an Astro app by configuring src/middleware.ts, protecting the /dashboard and /settings routes, authenticating SSR pages with Astro.locals.auth(), and ensuring static pages use a Clerk React island hydrated with client:load.”

Frequently Asked Questions about clerk-astro-patterns

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

FAQPage Schema
How do I secure SSR routes in an Astro app with Clerk middleware?

To secure SSR routes in Astro with Clerk, configure `clerkMiddleware` and `createRouteMatcher` in `src/middleware.ts` to guard specific paths like `/dashboard` and `/settings`, redirecting unauthenticated requests appropriately.

How does Clerk authentication work for prerendered Astro pages that skip middleware?

Clerk authentication for prerendered Astro pages works by using React Clerk hooks like `useAuth` within a `client:load` island component, ensuring the client-side UI reacts to sign-in state even when server middleware is bypassed.

How do I validate authenticated access inside Astro API routes using Clerk?

Validate authenticated access inside Astro API routes using Clerk by calling `context.locals.auth()` to check the session and returning the correct HTTP status codes for unauthenticated or unauthorized requests.

How do I handle organization-aware access and missing active organizations in Astro?

Handle organization-aware access in Astro by reading the authenticated context via `Astro.locals.auth()` and implementing redirects for requests missing an active organization or a valid user session.

Can I use Clerk React hooks in client-hydrated Astro islands?

Yes, you can use Clerk React hooks such as `useUser` in client-hydrated Astro islands with `client:load` to ensure static and prerendered pages correctly react to the user's sign-in state.

What is the best way to read authenticated context in Astro SSR pages with Clerk?

The best way to read authenticated context in Astro SSR pages with Clerk is by calling `Astro.locals.auth()` directly within the page component to retrieve session data and enforce route protection.