clerk
Skill4 starsChanged 11 months agoMITView on GitHub
Clerk authentication integration for Astro/Next.js. Use when implementing authentication, handling Clerk middleware, testing with Playwright, or debugging auth issues. Trigger phrases include "Clerk auth", "sign in", "authentication", "middleware", "E2E testing with Clerk".
- Reads credentials
16 other projects use a version of this.
Adopted by
Most recently taken up by
- oaknational/oak-open-curriculum-ecosystemtheir own version, first seen Nov 29, 2026
- Ghost7802/awesome-api-skillstheir own version, first seen Nov 29, 2026
- AdiRishi/promptrctheir own version, first seen Nov 29, 2026
- Azmainn/base-learnertheir own version, first seen Nov 15, 2026
- flowcopilot/skillstheir own version, first seen Nov 15, 2026
- nnawar706/fluentotheir own version, first seen Nov 8, 2026
Used with
Projects with it also use clerk-custom-ui, clerk-nextjs-patterns and 10 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
elpiartheraSkillclerk-custom-ui
Customize Clerk component appearance - themes, layout, colors, fonts, CSS. Use for appearance styling, visual customization, branding.
Also in 10 projects that use this
elpiartheraSkillclerk-nextjs-patterns
Advanced Next.js patterns - middleware, Server Actions, caching with Clerk.
Also in 8 projects that use this
sanketbodkeSkillclerk-billing
Clerk Billing for subscription management - render Clerk's PricingTable
Also in 4 projects that use this
AdiRishiSkillclerk-tanstack-patterns
'TanStack React Start auth patterns with @clerk/tanstack-react-start
Also in 4 projects that use this
oaknationalSkillclerk-backend-api
Clerk Backend REST API explorer and executor. Browse tags, inspect endpoint schemas, and execute authenticated requests. Use when listing users, managing organizations, or calling any Clerk API endpoint.
Also in 7 projects that use this
oaknationalSkillclerk-cli
Operate the Clerk CLI (`clerk` binary) for authentication, user/org/session management, impersonation, local webhook testing, deploy verification, instance config, env keys, feature toggles, and any Clerk Backend, Platform, or Frontend API call. Use when the user mentions Clerk management tasks, "list clerk users", "impersonate a user", "test webhooks locally", "enable orgs", "enable billing", "clerk env pull", "clerk doctor", "clerk deploy", "clerk api", or any ad-hoc Clerk API request. Prefer the CLI over raw HTTP: it handles auth, key resolution, app/instance targeting, and formatting automatically.
Also in 5 projects that use this
- +4 more from elpiarthera
Other versions16 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 16
- adrianhajdin/ghost-ai★ 332same name
- clerk/skills★ 95same name
- HamedMP/matrix-os★ 90same name
- delorenj/skills★ 17same name
- G1Joshi/Agent-Skills★ 15same name
- Ghost7802/awesome-api-skills★ 13same name
- oaknational/oak-open-curriculum-ecosystem★ 10same name
- flowcopilot/skills★ 3same name
- AdiRishi/promptrc★ 2same name
- Aymenjdily/biblion-testing-agentic-ai-skills★ 2same name
6 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
The file
We split this skill into the rules it gives an agent
7 rules. 0 are common, 4 are distinctive, and 3 are very specific to this project.
0
Common
Most of the 16 edited versions we read keep these.
4
Distinctive
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 64
Use actual UI sign-in flow with Clerk's +clerk_test email feature:
- Distinctive: line 291
Fix: Use UI-based sign-in flow with testing tokens:
- Very specific: line 311
Fix: Use +clerk_test email suffix with Clerk's magic test code:
- Distinctive: line 324
Fix: Use client-side redirect:
- Distinctive: line 339
Fix: Ensure SSR mode for protected routes:
- Very specific: line 372
If your local .env works but CI fails, sync your secrets:
- Distinctive: line 395
Fix: Update GitHub Secrets with correctly formatted emails.
“Common” is measured: kept by at least 6 in 10 of the 16 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Clerk Authentication Skill
Comprehensive guide for implementing and testing Clerk authentication, with special focus on Astro SSR integration and Playwright E2E testing.
Key Concepts
Clerk Architecture in Astro
┌─────────────────────────────────────────────────────────────┐
│ Browser (Client) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Clerk Frontend SDK (@clerk/astro) │ │
│ │ - Manages client-side session state │ │
│ │ - Provides <SignIn>, <UserButton> components │ │
│ │ - Sets localStorage tokens │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Server (Astro SSR) │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ clerkMiddleware (@clerk/astro/server) │ │
│ │ - Validates HTTPOnly session cookies │ │
│ │ - Runs BEFORE any custom middleware logic │ │
│ │ - Sets Astro.locals.auth() │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘Critical Understanding: Clerk's middleware validates sessions at the wrapper level BEFORE your callback executes. You cannot bypass authentication inside the middleware callback.
Session Types
| Session Type | Created By | Server Validated | Use Case |
| HTTPOnly Cookie | UI sign-in flow | ✅ Yes | Production, E2E tests |
| Client-side | @clerk/testing signIn() | ❌ No | Unit tests only |
| Backend API | sessions.create() | ⚠️ Partial | Limited use |
E2E Testing with Playwright
The Problem
@clerk/testing's programmatic clerk.signIn() creates client-side sessions only. These are NOT recognized by Clerk's server-side middleware in Astro/Next.js SSR applications.
// ❌ This creates client-side session only - won't pass middleware
await clerk.signIn({
page,
signInParams: { strategy: 'password', identifier: email, password }
});
// User appears logged in (UserButton shows), but server redirects to /sign-inThe Solution: UI-Based Sign-In with Test Emails
Use actual UI sign-in flow with Clerk's +clerk_test email feature:
// ✅ This creates real server-validated session
// 1. Navigate to sign-in with testing token (bypasses bot detection)
await page.goto(`/sign-in?__clerk_testing_token=${testingToken}`);
// 2. Fill in email (MUST contain +clerk_test)
await page.fill('input[name="identifier"]', 'user+clerk_test@example.com');
await page.click('button:has-text("Continue")');
// 3. Fill in password
await page.fill('input[type="password"]', password);
await page.click('button:has-text("Continue")');
// 4. Handle device verification with magic code
// See: clerk.com/docs/guides/development/testing/test-emails-and-phones
await enterVerificationCode(page, CLERK_TEST_VERIFICATION_CODE);Test Email Magic Code (CRITICAL for E2E/CI)
⚠️ CRITICAL: Test user emails MUST contain
+clerk_testfor automated testing to work. Without this suffix, Clerk requires real email verification which breaks CI/CD pipelines.
Any email with +clerk_test suffix is treated specially by Clerk:
- No actual email sent for verification
- Clerk's magic test code always works for any verification step
- Real users unaffected - normal verification for non-test emails
- Works in both development and production Clerk instances
Valid test email formats:
john+clerk_test@gmail.com✅test+clerk_test_admin@example.com✅user+clerk_test_member@company.com✅
Invalid for automated testing:
john+admin@gmail.com❌ (noclerk_testin address)john_clerk_test@gmail.com❌ (must use+plus-addressing)clerktest@gmail.com❌ (must use+clerk_testsuffix format)
Get the verification code: See Clerk's Test Emails Documentation for the magic verification code that works with +clerk_test emails.
💡 CI/CD Tip: Store test user emails in environment variables/secrets. Ensure all contain
+clerk_test:TEST_ADMIN_EMAIL=user+clerk_test_admin@gmail.com TEST_MEMBER_EMAIL=user+clerk_test_member@gmail.com
Testing Token
Get a testing token to bypass bot detection:
import { createClerkClient } from '@clerk/backend';
const clerkClient = createClerkClient({
secretKey: process.env.CLERK_SECRET_KEY,
});
const token = await clerkClient.testingTokens.createTestingToken();
// Use as: /sign-in?__clerk_testing_token=${token.token}Complete E2E Auth Setup
// tests/e2e/global-setup.ts
import { createClerkClient } from '@clerk/backend';
const clerkClient = createClerkClient({
secretKey: process.env.CLERK_SECRET_KEY,
});
async function authenticateUser(page, email, password, storagePath) {
// 1. Get testing token
const { token } = await clerkClient.testingTokens.createTestingToken();
// 2. Navigate with token
await page.goto(`/sign-in?__clerk_testing_token=${token}`);
// 3. Fill email (must have +clerk_test)
await page.fill('input[name="identifier"]', email);
await page.click('button:has-text("Continue")');
// 4. Fill password
await page.fill('input[type="password"]', password);
await page.click('button:has-text("Continue")');
// 5. Handle device verification (code from Clerk docs)
// See: clerk.com/docs/guides/development/testing/test-emails-and-phones
await page.waitForTimeout(2000);
if (page.url().includes('factor-two')) {
const code = process.env.CLERK_TEST_CODE; // From Clerk docs
const inputs = page.locator('input[inputmode="numeric"]');
for (let i = 0; i < 6; i++) {
await inputs.nth(i).fill(code[i]);
}
}
// 6. Wait for redirect and save session
await page.waitForURL(url => !url.includes('/sign-in'));
await page.context().storageState({ path: storagePath });
}Middleware Configuration
Basic Protected Routes
// src/middleware.ts
import { clerkMiddleware, createRouteMatcher } from "@clerk/astro/server";
const isPublicRoute = createRouteMatcher([
"/",
"/sign-in(.*)",
"/sign-up(.*)",
"/api/webhooks/(.*)",
]);
export const onRequest = clerkMiddleware((auth, context) => {
const { userId } = auth();
if (isPublicRoute(context.request)) {
return; // Allow public routes
}
if (!userId) {
return auth().redirectToSignIn();
}
});Role-Based Access
// Check role inside middleware callback
export const onRequest = clerkMiddleware(async (auth, context) => {
const { userId } = auth();
if (!userId) {
return auth().redirectToSignIn();
}
// Check admin routes
if (context.request.url.includes('/admin')) {
const member = await memberQueries.findByClerkId(userId);
if (member?.role !== 'admin') {
return context.redirect('/unauthorized');
}
}
});Common Patterns
Get Current User in Astro Pages
// src/pages/dashboard.astro
---
const auth = Astro.locals.auth();
const { userId, sessionClaims } = auth;
if (!userId) {
return Astro.redirect('/sign-in');
}
// Get user data from your database
const member = await memberQueries.findByClerkId(userId);
---Client-Side Auth Check
// For pre-rendered pages that need client-side auth
<script>
function checkAuth() {
if (window.Clerk?.loaded && !window.Clerk.user) {
window.Clerk.redirectToSignIn({ redirectUrl: window.location.href });
}
}
// Poll until Clerk loads
const interval = setInterval(() => {
if (window.Clerk?.loaded) {
clearInterval(interval);
checkAuth();
}
}, 100);
</script>Webhook Handling
// src/pages/api/webhooks/clerk.ts
import { Webhook } from 'svix';
export const POST: APIRoute = async ({ request }) => {
const payload = await request.text();
const headers = Object.fromEntries(request.headers);
const wh = new Webhook(process.env.CLERK_WEBHOOK_SECRET);
const event = wh.verify(payload, headers);
switch (event.type) {
case 'user.created':
// Create member record
break;
case 'user.updated':
// Sync user data
break;
}
return new Response('OK', { status: 200 });
};Troubleshooting
"Session not recognized by server"
Cause: Using @clerk/testing programmatic sign-in which only creates client-side sessions.
Fix: Use UI-based sign-in flow with testing tokens:
await page.goto(`/sign-in?__clerk_testing_token=${token}`);
// Then fill in the actual form"Bot traffic detected"
Cause: Clerk's bot protection blocking automated requests.
Fix: Include testing token in URL:
const token = await clerkClient.testingTokens.createTestingToken();
await page.goto(`/sign-in?__clerk_testing_token=${token.token}`);"Device verification required"
Cause: Clerk requires email verification from new devices.
Fix: Use +clerk_test email suffix with Clerk's magic test code:
// Email MUST contain +clerk_test for magic code to work
const email = 'user+clerk_test_admin@gmail.com';
// Get the code from: clerk.com/docs/guides/development/testing/test-emails-and-phonesCommon mistake: Using emails like user+admin@gmail.com without clerk_test - the magic code won't work!
"redirectToSignIn not working"
Cause: Page is pre-rendered (SSG) so server-side redirect doesn't work.
Fix: Use client-side redirect:
// In page frontmatter
export const prerender = true; // or remove for SSR
// In client script
if (!window.Clerk?.user) {
window.Clerk?.redirectToSignIn();
}Middleware Not Running
Cause: Route might be pre-rendered or middleware configuration issue.
Fix: Ensure SSR mode for protected routes:
// astro.config.mjs
export default defineConfig({
output: 'server', // or 'hybrid'
});CI/CD Integration
GitHub Actions Setup
# .github/workflows/e2e-auth.yml
- name: Run authenticated E2E tests
env:
CLERK_SECRET_KEY: ${{ secrets.TEST_CLERK_SECRET_KEY }}
# CRITICAL: Emails MUST contain +clerk_test
TEST_ADMIN_EMAIL: ${{ secrets.TEST_ADMIN_EMAIL }}
TEST_ADMIN_PASSWORD: ${{ secrets.TEST_ADMIN_PASSWORD }}
run: npx playwright testSecret Configuration Checklist
- ✅ Create test users in Clerk with
+clerk_testemails - ✅ Set passwords for test users (Clerk Dashboard → Users)
- ✅ Store email/password pairs as GitHub Secrets
- ✅ Verify emails contain
+clerk_testsubstring - ✅ Test locally before pushing to CI
Syncing Local to CI
If your local .env works but CI fails, sync your secrets:
# Script to update GitHub Secrets from .env
source .env
gh secret set TEST_ADMIN_EMAIL --body "$TEST_ADMIN_EMAIL"
gh secret set TEST_ADMIN_PASSWORD --body "$TEST_ADMIN_PASSWORD"
# Repeat for other test users...Common CI Failure: "Verification code failed"
Symptom: Local tests pass, CI tests fail at device verification step.
Root Cause: GitHub Secrets have emails WITHOUT +clerk_test:
# ❌ Wrong - magic code won't work
TEST_ADMIN_EMAIL=user+admin@gmail.com
# ✅ Correct - magic code will work
TEST_ADMIN_EMAIL=user+clerk_test_admin@gmail.comFix: Update GitHub Secrets with correctly formatted emails.
Environment Variables
# Required for Clerk
PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_xxx
CLERK_SECRET_KEY=sk_test_xxx
CLERK_WEBHOOK_SECRET=whsec_xxx
# For E2E testing - MUST contain +clerk_test
TEST_ADMIN_EMAIL=user+clerk_test_admin@gmail.com
TEST_ADMIN_PASSWORD=xxx
TEST_MEMBER_EMAIL=user+clerk_test_member@gmail.com
TEST_MEMBER_PASSWORD=xxxPackage Reference
| Package | Purpose |
@clerk/astro | Astro integration (components, middleware) |
@clerk/backend | Server-side operations (testing tokens, user management) |
@clerk/testing | Test utilities (limited - client-side only) |
svix | Webhook signature verification |
References
- Clerk Astro Integration
- Clerk Testing with Playwright
- Test Emails and Phones
- Testing Tokens
- Clerk Middleware
Last updated: December 22, 2025 Added: CI/CD integration patterns and +clerk_test email requirements
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

