P
Prisma7mo ago
crims

NextAuth 5> with PrismaPostgres & Edge Middleware

Hi there, has anyone figured out how to get Prisma Postgres working with NextAuth 5? I am using accelerate
import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'

const globalForPrisma = global as unknown as {
prisma: PrismaClient
}

const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

export default prisma
import { PrismaClient } from '@prisma/client'
import { withAccelerate } from '@prisma/extension-accelerate'

const globalForPrisma = global as unknown as {
prisma: PrismaClient
}

const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

export default prisma
And got authentication working fine when I am not running edge middleware. In the (outdated) documentation using accelerate is suggested as an alternative to the adapters so I kind of figured everything would work. But in my middleware.ts
export { auth as middleware } from "@/auth"
export { auth as middleware } from "@/auth"
Will cause a lot of errors
⨯ Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).
If this is unexpected, please open an issue: https://pris.ly/prisma-prisma-bug-report
at [project]/lib/prisma.ts [middleware-edge] (ecmascript) (lib/prisma.ts:8:60)
at [project]/auth.ts [middleware-edge] (ecmascript) (auth.ts:7:0)
6 | }
7 |
> 8 | const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())
| ^
9 |
10 | if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
11 |
⨯ Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).
If this is unexpected, please open an issue: https://pris.ly/prisma-prisma-bug-report
at [project]/lib/prisma.ts [middleware-edge] (ecmascript) (lib/prisma.ts:8:60)
at [project]/auth.ts [middleware-edge] (ecmascript) (auth.ts:7:0)
6 | }
7 |
> 8 | const prisma = globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())
| ^
9 |
10 | if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
11 |
It worked fine when I was using Neon Postgres with the official adapters and I would hate to move back lol. Things I've tried - use canary next with nodeMiddleware flags - move prisma instantiation into auth.js file
export const { handlers, auth, signIn, signOut } = NextAuth(() => {
const prisma = new PrismaClient().$extends(withAccelerate())
return ({
adapter: PrismaAdapter(prisma),
export const { handlers, auth, signIn, signOut } = NextAuth(() => {
const prisma = new PrismaClient().$extends(withAccelerate())
return ({
adapter: PrismaAdapter(prisma),
This removes the mention of auth.ts in the logs but still errors on @/lib/prisma.ts Any tips?
2 Replies
Prisma AI Help
Prisma AI Help7mo ago
You chose to debug with a human. They'll tinker with your query soon. If you get curious meanwhile, hop into #ask-ai for a quick spin!
crims
crimsOP7mo ago
Also: the official example (from prisma github) uses the Next Auth CredentialsProvider without a middleware file. And (i think by default) a JWT strategy which does not call the database; might be mistaken on this though.

Did you find this page helpful?