middleware-protection
Route protection and authorization patterns for Clerk middleware. Use when implementing route guards, protecting API routes, configuring middleware matchers, setting up role-based access control, creating auth boundaries, or when user mentions middleware, route protection, auth guards, protected routes, public routes, matcher patterns, or authorization middleware.
What this skill does
# Middleware Protection Comprehensive route protection and authorization patterns for Clerk middleware in Next.js applications. Provides middleware configuration, route matchers, role-based access control, and authentication boundaries. ## Core Concepts ### Middleware Architecture - **Edge Runtime**: Clerk middleware runs on Cloudflare Workers/Vercel Edge - **Request Interception**: Middleware executes before route handlers - **Auth State**: Access to authentication state via `auth()` helper - **Matcher Patterns**: Configure which routes middleware applies to ### Route Protection Levels 1. **Public Routes**: Accessible without authentication (sign-in, sign-up, landing pages) 2. **Protected Routes**: Require authentication (dashboards, user profiles) 3. **Organization Routes**: Require organization membership 4. **Role-Based Routes**: Require specific roles or permissions ### Security Principles - **Deny by Default**: All routes protected unless explicitly made public - **Defense in Depth**: Middleware + server component checks + API route guards - **Session Validation**: Automatic token validation on every request - **CSRF Protection**: Built-in protection against cross-site request forgery ## Instructions ### Basic Middleware Setup 1. **Create middleware.ts in project root** - Import `clerkMiddleware` from `@clerk/nextjs/server` - Export default middleware function - Configure matcher for routes to protect 2. **Configure Public Routes** - Define routes accessible without authentication - Use glob patterns for route matching - Include sign-in/sign-up pages as public 3. **Set Protected Routes** - Specify which routes require authentication - Use route groups for organization - Apply different protection levels ### Advanced Patterns 1. **Role-Based Access Control** - Check user roles in middleware - Redirect based on permissions - Implement organization-level permissions 2. **Conditional Route Protection** - Apply different rules based on route patterns - Check custom metadata - Implement feature flags 3. **API Route Protection** - Secure API endpoints with middleware - Validate session tokens - Check permissions before processing 4. **Multi-Tenant Protection** - Organization-scoped routes - Tenant isolation - Cross-organization access prevention ### Testing Protection 1. **Test Authentication Boundaries** - Verify unauthenticated redirects - Check protected route access - Validate role requirements 2. **Test Edge Cases** - Token expiration handling - Invalid session handling - Missing organization membership ## Templates Use these templates for middleware implementation: ### Core Templates - `templates/middleware.ts` - Basic middleware configuration - `templates/route-matchers.ts` - Route matching patterns - `templates/role-based-middleware.ts` - Role-based access control ### Configuration Templates - `templates/public-routes-config.ts` - Public route definitions - `templates/protected-routes-config.ts` - Protected route setup - `templates/api-middleware-config.ts` - API route protection ### Advanced Templates - `templates/organization-middleware.ts` - Organization-scoped protection - `templates/conditional-middleware.ts` - Conditional route logic - `templates/custom-redirects.ts` - Custom redirect handling ## Scripts Use these scripts for middleware setup and testing: - `scripts/generate-middleware.sh` - Generate middleware.ts with configuration - `scripts/configure-routes.sh` - Setup route protection patterns - `scripts/test-protection.sh` - Test authentication guards and boundaries - `scripts/validate-middleware.sh` - Validate middleware configuration ## Examples See complete examples in the `examples/` directory: ### Basic Examples - `examples/basic-middleware.ts` - Simple middleware setup - `examples/public-private-routes.ts` - Public vs protected routes - `examples/api-middleware.ts` - API route protection ### Advanced Examples - `examples/role-based-protection.ts` - Role-based access control - `examples/organization-routes.ts` - Organization-scoped routes - `examples/conditional-routing.ts` - Conditional protection logic - `examples/custom-auth-flow.ts` - Custom authentication flows ### Testing Examples - `examples/middleware-tests.ts` - Middleware unit tests - `examples/integration-tests.ts` - Full protection integration tests ## Security Best Practices ### API Key Handling **CRITICAL**: When generating middleware configuration: - ❌ NEVER hardcode CLERK_SECRET_KEY or NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY - ❌ NEVER include real API keys in examples - ✅ ALWAYS use placeholders: `your_clerk_secret_key_here` - ✅ ALWAYS read from environment variables: `process.env.CLERK_SECRET_KEY` - ✅ ALWAYS add `.env*` to `.gitignore` (except `.env.example`) - ✅ ALWAYS document where to obtain keys from Clerk Dashboard ### Middleware Security 1. **Validate All Requests**: Don't skip middleware on any protected routes 2. **Check Session Validity**: Always validate session tokens 3. **Implement Rate Limiting**: Protect against brute force attacks 4. **Log Security Events**: Track authentication failures and suspicious activity 5. **Use HTTPS Only**: Never run authentication over HTTP in production ## Requirements **Next.js Version:** - Next.js 13.4+ (App Router support) - Next.js 12+ (Pages Router support) **Clerk SDK:** - @clerk/nextjs 4.0+ (latest stable) - Node.js 16+ **Configuration Files:** - `.env.local` with Clerk environment variables - `middleware.ts` in project root - `.gitignore` protecting secrets ## Common Patterns ### Pattern 1: Public Landing + Protected Dashboard ```typescript // Public: /, /about, /pricing // Protected: /dashboard/*, /profile/* // Matcher: Protect everything except public routes ``` ### Pattern 2: API Route Protection ```typescript // Protect all /api/* except /api/webhooks/clerk // Validate session tokens on protected endpoints // Return 401 for unauthenticated requests ``` ### Pattern 3: Organization-Scoped Routes ```typescript // Require organization membership for /org/* // Check active organization in middleware // Redirect to organization selection if needed ``` ### Pattern 4: Role-Based Access ```typescript // Check user roles in middleware // Redirect based on permissions (admin vs user) // Implement feature-specific access control ``` ## Troubleshooting ### Common Issues 1. **Middleware Not Running** - Check matcher configuration - Verify middleware.ts location (project root) - Ensure Next.js version supports middleware 2. **Infinite Redirect Loops** - Sign-in page must be public - Check redirect logic in middleware - Verify afterSignInUrl configuration 3. **Protected Routes Accessible** - Verify matcher includes routes - Check auth state validation - Ensure middleware executes before route 4. **Session Not Found** - Check environment variables loaded - Verify Clerk keys are correct - Ensure cookies not blocked --- **Purpose**: Provide comprehensive middleware protection patterns for Clerk authentication **Load when**: Implementing route guards, protecting routes, setting up middleware, configuring auth boundaries
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.