clerk-astro-patterns
/clerk-astro-patterns
Pokazuje wzorce logowania Clerk w Astro: middleware, strony SSR, komponenty wyspowe i chronione trasy API.
Ocena i bezpieczeństwo
- Pliki w paczce
- 14
- Skrypty
- 2
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 2
Brak sygnałów. Repozytorium zmienione 10.11.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
- 2 skrypty w paczce. Przejrzyj je przed instalacją, bo Claude może je uruchomić.
Skrypty: templates/astro-basic-auth/astro.config.mjs, templates/astro-basic-auth/src/middleware.ts
Co robi i kiedy
Pokazuje wzorce logowania Clerk w Astro: middleware, strony SSR, komponenty wyspowe i chronione trasy API.
Oryginalny opis (EN)
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.
Jak działa
- What Do You Need?
- Mental Model
- Setup
- SSR Page Auth
- API Route Auth
- Common Pitfalls
- Import Map
- Env Variables
- See Also
- Docs
Podgląd SKILL.md
---
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)
Pliki w paczce (14)
- SKILL.md 4 KB
- evals/evals.json 6 KB
- references/api-routes.md 2 KB
- references/astro-react.md 2 KB
- references/island-components.md 2 KB
- references/middleware.md 2 KB
- references/ssr-pages.md 2 KB
- templates/astro-basic-auth/astro.config.mjs 1 KB
- templates/astro-basic-auth/package.json 1 KB
- templates/astro-basic-auth/tsconfig.json 1 KB
- templates/astro-basic-auth/src/middleware.ts 1 KB
- templates/astro-basic-auth/src/layouts/Layout.astro 1 KB
- templates/astro-basic-auth/src/pages/index.astro 1 KB
- templates/astro-basic-auth/src/pages/sign-in.astro 1 KB
Wersje
| Repozytorium | Wynik | Licencja |
|---|---|---|
| Ta strona · clerk/skills | 100 | MIT |
| midudev/autoskills | 100 | MIT |
Migawka bazy z 13.11.2026 · wersja paczki a2206eb5a46d (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 2 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100clerk-nuxt-patterns
Pokazuje wzorce uwierzytelniania w Nuxt 3 z Clerk: middleware, composable, serwerowe trasy API i SSR. - 100clerk-nuxt-patterns
Podpowiada wzorce uwierzytelniania Clerk w Nuxt 3: middleware, composable, trasy API po stronie serwera i ochronę tras. - 90clerk-auth
Pokazuje wzorce uwierzytelniania Clerk: middleware, organizacje, webhooki i synchronizację użytkowników. - 100clerk-react-router-patterns
Pokazuje wzorce Clerk dla React Router v7 i v8: loadery, chronione trasy, middleware i dane użytkownika po stronie serwera. - 90clerk-auth
Dostarcza wzorce implementacji uwierzytelniania Clerk: middleware, organizacje, webhooki i synchronizacja użytkowników, przy dodawaniu logowania i rejestracji. - 90clerk-auth
Dostarcza wzorce implementacji uwierzytelniania Clerk: middleware, organizacje, webhooki i synchronizacja użytkowników, przy dodawaniu logowania i rejestracji.