Next.js Discord

Discord Forum

Best practice for route protection in Next.js + Auth.js + Prisma (avoiding Prisma Edge runtime issue

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Hi everyone, I’m a beginner with the Next.js + Auth.js + Prisma stack and I’m stuck on route protection.

I already set up GitHub login successfully (https://github.com/harley61p/nextjs-authjs-prisma-example.git).
Now I want to protect certain routes, so that only logged-in users can access them.

I tried using middleware.ts, but Prisma throws an error:
PrismaClientValidationError: In order to run Prisma Client on edge runtime...

I understand middleware runs on the Edge runtime, so Prisma can’t be used there.
But what’s the recommended way to implement elegant route protection with this stack?

Should I only check session/token inside middleware.ts and move DB checks to server components / API routes?

Or is there a better practice?

Which Discord would be best to ask this (Next.js, Prisma, or Auth.js)?
Thanks a lot! 🙏

1 Reply

Pacific sand lance
1. /(auth)/layout.tsx combined with redirect/authInterrupts
2. HOC e.g. withAuth then do sth like export default withAuth(Page)

both should work, with 1st approach all routes under /(auth) are protected, with 2nd approach you have to manually wrap all protected pages in withAuth HOC