How to use NextAuth PrismaAdapter with Middleware in a Hybrid Edge + Node.js Runtime setup (Edge Runtime Error with DB Sessions) #13149
Unanswered
thissidemayur
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on a Next.js application using NextAuth.js with PrismaAdapter connected to a PostgreSQL database (via Neon.tech). My session strategy is database sessions, and everything works fine in API routes and pages.
However, when I try to use
export { auth as middleware } from "@/lib/auth"
; in middleware.ts for protecting routes, I get the following error:[auth][error] SessionTokenError: Read more at https://errors.authjs.dev#sessiontokenerror [auth][cause]: Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).
### What I understood so far (Correct me if wrong):
Prisma ORM is designed to run in Node.js runtime, while middleware.ts in Next.js runs in Edge Runtime.
This causes PrismaClient to fail because Edge Runtime doesn’t support Node.js libraries like Prisma.
NextAuth’s PrismaAdapter inherently needs Node.js (for DB sessions) and can’t work inside middleware (which expects edge-safe code).
My Use Case:
I want to protect routes via Middleware-based auth checks.
I’m using DB sessions strategy (not JWT) for persistence, so I need PrismaAdapter.
I want to architect a hybrid Edge + Node runtime setup where Middleware can check sessions efficiently.
Beta Was this translation helpful? Give feedback.
All reactions