All packsMulti-Tenant Auth & RBAC CoreSolve
nextjs middleware edge runtime cannot use pg postgres driver
Middleware runs on the Edge runtime -- pg and argon2 don't
Next.js middleware runs on the Edge runtime by default, which can't load the pg driver or native argon2 -- auth logic has to live in node-runtime route handlers instead.
What goes wrong
Next.js middleware executes on the Edge runtime, a restricted JavaScript environment that doesn't support Node-native modules or TCP sockets. Code that tries to check a session by querying Postgres directly inside middleware.ts -- a natural place to put "run this check before every request" -- fails at build or runtime, because the pg driver and native argon2 bindings simply cannot load there.
Why agents get it wrong
Middleware is explicitly designed and documented as the place to run logic before a request reaches a route -- "gate every request through one auth check" reads like exactly what middleware is for, and the Edge-runtime restriction is a platform-level constraint that isn't visible from the application code being written, only from trying to run it.
What Multi-Tenant Auth & RBAC Core pre-decides
Core decision 19 states there is no middleware.ts in this pack at all -- every auth check runs inside node-runtime route handlers via a shared requireSession() helper, which is free to use pg and native argon2 because the node runtime supports both. The tradeoff (no request-level gating before routing) is accepted explicitly rather than worked around with an Edge-compatible reimplementation of session lookup.
Source: ARCHITECTURE.md core decision 19