From clerk-pack
Implement session management and middleware with Clerk. Use when managing user sessions, configuring route protection, or implementing token refresh logic. Trigger with phrases like "clerk session", "clerk middleware", "clerk route protection", "clerk token", "clerk JWT".
How this skill is triggered — by the user, by Claude, or both
Slash command
/clerk-pack:clerk-core-workflow-bThis skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
- [Overview](#overview)
Implement session management and route protection with Clerk middleware. Covers Next.js middleware configuration, API route protection, role-based access control, and organization-scoped sessions.
@clerk/nextjs package installedCreate middleware.ts at project root. Define public routes (landing, sign-in, webhooks) and admin routes. Use clerkMiddleware with auth.protect() for private routes and role-based protection for admin routes.
Use auth() in route handlers to get userId, orgId, and has() for permission checks. Return 401/403 for unauthorized/insufficient permissions.
Access session data, user profile, and generate JWT tokens for external APIs (Supabase, etc.) using getToken({ template: 'name' }).
Use auth() in server components with redirect('/sign-in') for unauthenticated users. Check roles/permissions with has() for conditional UI rendering.
See detailed implementation for complete middleware config, API route examples, session claims, server component patterns, and role-based navigation.
| Issue | Cause | Solution |
|---|---|---|
| Middleware redirect loop | Public route not in matcher | Add route to isPublicRoute |
| 401 on API route | Token not forwarded | Ensure fetch includes credentials |
| Missing org context | User not in organization | Check orgId before org-scoped ops |
| Session expired | Token TTL exceeded | Configure session lifetime in dashboard |
const { has } = await auth();
if (has({ permission: 'org:data:write' })) {
// User can write data in this organization
}
npx claudepluginhub p/ktiseos-nyx-clerk-pack-plugins-saas-packs-clerk-packGuides creation and editing of skills using test-driven development with pressure scenarios and subagents to verify agent compliance.
Processes, converts, OCRs, extracts, redacts, signs, and fills documents via the Nutrient DWS API. Supports PDF, DOCX, XLSX, PPTX, HTML, and images.