Replies: 1 comment
-
|
This is likely a memory issue caused by the Supabase adapter + Prisma client initialization loop. Here's what typically happens: Common causes1. Prisma client instantiation in dev mode: In development, Next.js hot-reloads modules, which can create multiple Prisma client instances. Each one opens a connection pool. With the Supabase adapter, this compounds quickly. Fix — use a singleton: // lib/prisma.ts
import { PrismaClient } from "@prisma/client";
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};
export const prisma = globalForPrisma.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== "production") {
globalForPrisma.prisma = prisma;
}2. Direct Postgres connection to Supabase without pooling: If you're connecting directly to Supabase Postgres (port 5432), each Prisma client opens its own connection pool. Use the connection pooler URL instead (port 6543): # .env
# Use pooler URL (port 6543), NOT direct (port 5432)
DATABASE_URL="postgresql://postgres.[ref]:[password]@aws-0-[region].pooler.supabase.com:6543/postgres?pgbouncer=true"
# Direct URL only for migrations
DIRECT_URL="postgresql://postgres.[ref]:[password]@aws-0-[region].pooler.supabase.com:5432/postgres"// schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}3. Auth.js v5 beta + adapter infinite loop: Some beta versions of Auth.js v5 had a bug where the session callback would trigger a database query, which would trigger the session callback again. Check your // auth.ts
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "./lib/prisma";
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [Google],
// IMPORTANT: Don't query the DB inside session callback
// if using the adapter — it already handles this
callbacks: {
session: ({ session, user }) => ({
...session,
user: {
...session.user,
id: user.id,
},
}),
},
});DebuggingCheck memory usage: NODE_OPTIONS="--max-old-space-size=4096" npm run devIf this prevents the crash, you were running out of heap memory. Check for connection leaks: # In Supabase Dashboard > Database > Connections
# See if connections spike when running the appMinimal working setup for Auth.js v5 + Supabase// auth.ts
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { SupabaseAdapter } from "@auth/supabase-adapter";
export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [Google],
adapter: SupabaseAdapter({
url: process.env.SUPABASE_URL!,
secret: process.env.SUPABASE_SERVICE_ROLE_KEY!,
}),
});Note: Use If the crash persists, share the error output / stack trace and I can narrow it down further. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I started with a new Next project using version 16 and want to setup next-auth ( v5 beta 30 ) for Google. For the database I want to use my Supabase instance ( with the schema described here https://authjs.dev/getting-started/adapters/supabase )
Reproduction sandbox: https://stackblitz.com/edit/stackblitz-starters-4xyfndf5
My .env file is following the required next-auth pattern
I started with the auth.ts file in the root dir and passed my own sign in page to the
pagesfieldand registered a route at
./app/api/auth/[...nextauth]/route.tsNext I created a
SessionProviderand added it to the root layout
Finally I got this sign-in page
Now when starting the application and navigating to http://localhost:3000/sign-in I click the "Sign in with Google" button for the first time. The URL immediately changes to http://localhost:3000/api/auth/signin?callbackUrl=%2F but nothing happens.
When I click a second time the system crashes with a runtime error
and the console shows
How can I fix this?
Often enough the whole machine freezes / crashes.
Beta Was this translation helpful? Give feedback.
All reactions