nextjs-auth0 icon indicating copy to clipboard operation
nextjs-auth0 copied to clipboard

PrismaClient is unable to run in this browser environment

Open jorge-riberi opened this issue 1 year ago • 2 comments

Checklist

  • [x] The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
  • [x] I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • [x] I have looked into the API documentation and have not found a suitable solution or answer.
  • [x] I have searched the issues and have not found a suitable solution or answer.
  • [x] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • [x] I agree to the terms within the Auth0 Code of Conduct.

Description

With this code

import { Auth0Client } from "@auth0/nextjs-auth0/server"; import {NextResponse} from "next/server"; import {PrismaClient} from "@prisma/client";

export const auth0 = new Auth0Client({ authorizationParameters: { scope: process.env.AUTH0_SCOPE, }, beforeSessionSaved: async (session) => { console.log('before oauth0 session', session); const prisma = new PrismaClient(); const roles = await prisma.userProfile.findFirst({ where: {sub: session.user.sub}, select: {roles: true} }); console.log('roles', roles); return session } });

I have an error

Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in ``).

How can I fix it?

Reproduction

Implement prisma client on beforeSessionSaved hook

Additional context

No response

nextjs-auth0 version

4.5.1

Next.js version

15.3.2

Node.js version

22.13.1

jorge-riberi avatar May 16 '25 03:05 jorge-riberi

Hello @jorge-riberi ,

Looks like it is happening because beforeSessionSaved is being evaluated in an edge environment, and Prisma is not compatible with the edge runtime. Prisma requires a Node.js environment to function properly.

Most likely it's triggered by const authRes = await auth0.middleware(request); used in middleware.ts.

You can add a debug log like console.log('Runtime:', process.env.NEXT_RUNTIME); inside the beforeSessionSaved hook to check whether it's being executed in the Edge Runtime.

  beforeSessionSaved: async (session) => {
    console.log("Running in Edge:", process.env.NEXT_RUNTIME); // Logs 'edge' if running in the Edge Runtime
    const prisma = new PrismaClient();
    const roles = await prisma.userProfile.findFirst({
      where: {sub: session.user.sub},
      select: {roles: true}
    });
    console.log('roles', roles);
    return session
  }

Solution:

This is not a bug with our SDK, so I can’t provide a definitive fix. However, here are a few helpful pointers you can explore:

  1. Next.js Middleware Runtime: Refer to the Next.js documentation on middleware runtime configuration. You can set the runtime to nodejs using an experimental option, though it's not the recommended solution.

  2. Split your Auth0 client configuration: Creating two separate Auth0Client instances:

    • One for Edge environments, which omits any server-only logic such as Prisma.
    • One for Node.js environments, where you can safely use Prisma in hooks like beforeSessionSaved. Since I’m not familiar with the specifics of your codebase, this approach may not be suitable in all cases. Please adopt it only if it aligns with your architecture.

nandan-bhat avatar May 16 '25 21:05 nandan-bhat

Hi @nandan-bhat, thanks a lot for your response. I will follow your instructions.

jorge-riberi avatar May 17 '25 14:05 jorge-riberi