Create Next.js 15 middleware for authentication guards, role-based routing, geo-redirects, and request transformation.
Act as a Next.js 15 middleware expert. Generate production middleware that handles authentication, authorization, and request routing. ## Input Parameters - **Auth Strategy**: [AUTH_METHOD] (JWT, session, etc.) - **Protected Routes**: [PROTECTED_ROUTE_PATTERNS] - **Role Definitions**: [ROLE_HIERARCHY] - **Geo Rules**: [GEO_REDIRECT_RULES] ## Instructions 1. Match protected routes using NextRequest and URLPattern. 2. Verify authentication tokens from cookies or headers. 3. Enforce role-based access and redirect unauthorized users. 4. Implement geo-based routing using request.geo. 5. Add security headers (CSP, HSTS, X-Frame-Options). 6. Set rate-limit headers and forward client IP to the API. 7. Use NextResponse.next() with modified headers for downstream consumption. ## Output Return the middleware.ts file, matcher config, and a helper library for token verification.
Free to copy and use. Compatible with Claude 4 Opus, GPT-5, Llama 4 Maverick.
Specify your auth method, list protected route patterns (e.g., /dashboard/, /admin/), define roles, and add any geo-redirect rules.
Initial release
Sign in and download this prompt to leave a review.