I am trying to implement authentication with authjs v5, drizzle, and neon db. I use the following dependencies:
“@auth/drizzle-adapter”: “^0.9.0”,
“drizzle-orm”: “^0.30.8”,
“next-auth”: “^5.0.0-beta.15”
The main files are auth.ts and auth.config.ts:
// auth.ts
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { eq } from "drizzle-orm";
import NextAuth from "next-auth";
import authConfig from "./auth.config";
import { db } from "./drizzle-app/db";
import { users } from "./drizzle-app/schema";
export const { handlers, auth, signIn, signOut } = NextAuth({
events: {
async linkAccount({ user }) {
await db
.update(users)
.set({ emailVerified: new Date() })
.where(eq(users.id, user.id!));
},
},
callbacks: {
async session({ session, token }) {
if (token.sub && session.user) {
session.user.id = token.sub;
}
return session;
},
async jwt({ token }) {
return token;
},
},
adapter: DrizzleAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});
// auth.config.ts
import type { NextAuthConfig } from "next-auth";
import Google from "next-auth/providers/google";
export default {
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
],
} satisfies NextAuthConfig;
Also, this is where I export the database:
// db.ts
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "@/drizzle-app/schema";
// neonConfig.fetchConnectionCache = true;
const sql = neon(process.env.DB_APP_URL!);
export const db = drizzle(sql, {
schema,
});
Finally, I export the route handlers in /api/auth/[…nextauth]/route.ts
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
The problem is that in the dev server (I haven’t tested live yet), the requests to api/auth/session occur in batches of 12 requests:
GET /api/auth/session 200 in 6085ms
GET /api/auth/session 200 in 5899ms
GET /api/auth/session 200 in 114ms
GET /api/auth/session 200 in 16ms
GET /api/auth/session 200 in 38ms
GET /api/auth/session 200 in 13ms
GET /api/auth/session 200 in 40ms
GET /api/auth/session 200 in 14ms
GET /api/auth/session 200 in 37ms
GET /api/auth/session 200 in 13ms
GET /api/auth/session 200 in 49ms
GET /api/auth/session 200 in 21ms
One duplication would make sense to me since the dev server usually sends two requests, but I can’t figure out why so many requests are sent. Any help would be much appreciated.