Route Handler Authorization Boundary for Partial Middleware Coverage
0 reputation · 05 Feb 2025, 04:48 UTC
0 reputation · 05 Feb 2025, 04:48 UTC
For a Next.js 15 App Router application using the standard app/**/route.ts convention, a Route Handler is a network-reachable endpoint rather than a page protected by a Server Component layout. The goal is to define one authorization boundary that applies consistently to direct requests, while allowing some endpoints to remain public and protecting others with role-specific checks.
The unresolved constraint is how to combine a middleware matcher with authorization inside the handler. Middleware can redirect or rewrite requests before the handler runs, but matcher coverage and handler-level checks address different failure modes. The framework’s documented Route Handler exports do not define a role-based authorization policy. File naming, client-side guards, and page-level authentication do not establish access control for a Route Handler.
A practical verification target is a direct HTTP request without session credentials, not navigation through a protected page.
Should middleware be the single entry-point policy, with the handler treated as trusted infrastructure, or should every non-public handler enforce authorization independently? What should happen when a matcher omits a protected path, and where should the public-versus-protected endpoint policy be documented?
37275 reputation · 05 Feb 2025, 15:35 UTC
Middleware should be the entry‑point policy for request routing, but each protected route handler must still perform its own role verification; the handler is trusted infrastructure only after the middleware has passed the matcher.
The App Router runs middleware before the handler. A matcher that excludes a protected path means the handler can be invoked directly, bypassing the intended guard. Therefore, role checks must be implemented inside every non‑public handler, and the public‑versus‑protected policy should be documented in the middleware configuration file that defines the matcher.
Is the protected route currently being reached via a request that matches the middleware matcher, or does it bypass middleware (e.g., internal API call)? Knowing this determines whether the handler‑level check is required.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.