Next.js Discord

Discord Forum

Error: Middleware with Prisma

Unanswered
Axus posted this in #help-forum
Open in Discord
Hi, i used a middleware to check protected route in my nextJs app, but when when i did it, an error appeared with Prisma
the Error:
[auth][error] SessionTokenError: Read more at https://errors.authjs.dev#sessiontokenerror
[auth][cause]: Error: PrismaClient is not configured to run in Edge Runtime (Vercel Edge Functions, Vercel Edge Middleware, Next.js (Pages Router) Edge API Routes, Next.js (App Router) Edge Route Handlers or Next.js Middleware). In order to run Prisma Client on edge runtime, either:
- Use Prisma Accelerate: https://pris.ly/d/accelerate
- Use Driver Adapters: https://pris.ly/d/driver-adapters

See full error in attachement

My middleware.ts :

import { NextRequest, NextResponse } from "next/server";
import { GetUser } from "./lib/actionUser";
import { auth } from "./lib/auth";

export async function middleware(req: NextRequest) {
const session = await auth();

if (!session?.user) {
return NextResponse.redirect(new URL("/", req.url));
}

const userId = session.user.id;
if (typeof userId !== "string") {
return NextResponse.redirect(new URL("/", req.url));
}

const user = await GetUser(userId);
const url = req.nextUrl;
const forbiddenUrl = url.pathname.startsWith("/dashboard");

if (forbiddenUrl && user?.userRole !== "PRO") {
return NextResponse.redirect(new URL("/", req.url));
}

return NextResponse.next();
}

Im using neon.tech with Prisma

Middleware emplacement in attachement

Thanks to help me

26 Replies

@Axus Hi, i used a middleware to check protected route in my nextJs app, but when when i did it, an error appeared with Prisma the Error: [auth][error] SessionTokenError: Read more at https://errors.authjs.dev#sessiontokenerror [auth][cause]: Error: PrismaClient is not configured to run in Edge Runtime (Vercel Edge Functions, Vercel Edge Middleware, Next.js (Pages Router) Edge API Routes, Next.js (App Router) Edge Route Handlers or Next.js Middleware). In order to run Prisma Client on edge runtime, either: - Use Prisma Accelerate: https://pris.ly/d/accelerate - Use Driver Adapters: https://pris.ly/d/driver-adapters See full error in attachement My middleware.ts : import { NextRequest, NextResponse } from "next/server"; import { GetUser } from "./lib/actionUser"; import { auth } from "./lib/auth"; export async function middleware(req: NextRequest) { const session = await auth(); if (!session?.user) { return NextResponse.redirect(new URL("/", req.url)); } const userId = session.user.id; if (typeof userId !== "string") { return NextResponse.redirect(new URL("/", req.url)); } const user = await GetUser(userId); const url = req.nextUrl; const forbiddenUrl = url.pathname.startsWith("/dashboard"); if (forbiddenUrl && user?.userRole !== "PRO") { return NextResponse.redirect(new URL("/", req.url)); } return NextResponse.next(); } Im using neon.tech with Prisma Middleware emplacement in attachement Thanks to help me
Sloth bear
Middleware run on the edge runtime but seems like the prisma driver you are using doesn't work on edge runtime so you are getting this error
Hi thanks to anwser, i don't understand what is the prisma driver, and what sould i do to correct this bug ?
@Sloth bear Middleware run on the edge runtime but seems like the prisma driver you are using doesn't work on edge runtime so you are getting this error
I saw with chatGpt and he said 3 solutions:
1. i use the api route
2. I switch to drizzle (don't know how to use it, but gonna learn it for the next project)
3. i switch to a non edge function (i don't even know what is a edge function)
It don't work
@Axus It don't work
Sun bear
Depending on your version you can try:

const session = await auth();
Hi, i tried it , my middleware worked but the error
[auth][error] SessionTokenError: Read more at https://errors.authjs.dev#sessiontokenerror
[auth][cause]: Error: PrismaClient is not configured to run in Edge Runtime (Vercel Edge Functions, Vercel Edge Middleware, Next.js (Pages Router) Edge API Routes, Next.js (App Router) Edge Route Handlers or Next.js Middleware). In order to run Prisma Client on edge runtime, either:
- Use Prisma Accelerate: https://pris.ly/d/accelerate
- Use Driver Adapters: https://pris.ly/d/driver-adapters
The problem is more clear, but i don't know how to resolve it
I did a checkout integration with stripe, but there is a problem when i finish and valid the checkout, there is a small period where i am not connected (while i am) .
So it redirect to Home Page and dele te the checkout
i dont understand
did u fix ur prisma issue
Same problem
use prisma accelerate for the edge
@gin use prisma accelerate for the edge
ok, is it a good choice ?
@Axus ok, is it a good choice ?
never used it idk
whats your auth function?
I found the following problem, I noticed that when I validated the checkout stripe in my console, there was “Protected” (console.log() which comes from my middleware, which means, if the user is not logged in, he is redirected to the home page, which cancels the checkout session). This means there's a small period when I'm not deconnected at the end of the checkout (I know if you understand).
@gin whats your auth function?
Auth function:
import { PrismaAdapter } from "@auth/prisma-adapter";
import NextAuth from "next-auth";
import prisma from "./prisma";
import GoogleProvider from "next-auth/providers/google";
import { stripe } from "./stripe";

export const { auth, handlers, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [GoogleProvider],
events: {
createUser: async (message) => {
const userId = message?.user?.id;
const email = message?.user?.email;
const name = message?.user?.name;

if(!userId || !email){
return
}

const stripeCustomer = await stripe.customers.create({
email,
name: name ?? undefined,
})

await prisma.user.update({
where:{
id: userId
},
data: {
stripeCustomerId: stripeCustomer?.id
}
})
}
}
})
Ye
Im gonna try the Prisma Accelerate
Im just gonna learn Drizzle, im gonna convert all the app in drizzle, i need to pay for prisma accelerate and it is long to set up