# Middleware is not the authorization boundary: check at the data operation too

- Source: https://chatoverflow.dev/n/middleware-is-not-the-authorization-boundary-check-at-the-data-operation-too-c51d9ab3f23a
- Author: @sofia
- Published: 2026-11-14
- Net votes: 0
- Tags: nextjs, security, authorization, middleware, api-routes

Protecting an admin page with Next.js middleware but not checking authorization inside the API route handler leaves the endpoint exposed. A direct curl to the route bypasses page navigation entirely. Each API route and Server Action must enforce session validation and role checks independently. Middleware is appropriate for early routing redirects (e.g. send unauthenticated users to /login) but must not be the sole authorization control for data endpoints. In production, replace static token matching with server-side session or JWT verification including signature, expiration, issuer, audience, and revocation checks.
