-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AdapterError] NextAuth with Drizzle Adapter #12411
Comments
Hi @Maisaan , please take a look at this: |
Yes, been over that. Afaik, DrizzleAdapter is edge compatible, and i wish to use the database strategy because i want to have a finer control over the users, with permissions and roles. When you lazy initialise or split configs you loose database functionality and thats not how i want it. |
I encountered a similar error. I was able to resolve it after i removed the import of server side database interaction function from client side related hook. Hope this helps. |
That's good news! Could you please show me how you did it? Thanks! |
Hello @FabriBorgobello, I uploaded a branch I was implementing search feature, had to interact with database. Instead of directly using the database function (which caused the error for me as shown below), which is supposed to be used server side rather than client side, I implemented an api handler for it that indirectly works with the database function. |
Thanks a lot @xibluespider . I really appreciate it! |
You might be encountering this issue because NextAuth.js middleware is running in an Edge environment, but Drizzle (especially when using the postgres library) is designed to run in a Node.js environment. I’ve faced a similar issue before, and I resolved it by separating Edge-compatible middleware from the Drizzle Adapter setup. Here’s how I structured my NextAuth setup to ensure that authentication works properly without causing AdapterError: 1. authConfig.ts (Authentication Configuration - Adapter Based) import { NextAuthConfig } from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { accounts, neonDB, sessions, users, verificationTokens } from "./db/schema";
export default {
adapter: DrizzleAdapter(neonDB, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
verificationTokensTable: verificationTokens,
}),
// other options...
} satisfies NextAuthConfig; **2. ./db/schema.ts import { drizzle as neonDrizzle } from "drizzle-orm/neon-serverless";
const connectionString = process.env.DATABASE_URL;
export const neonDB = neonDrizzle(connectionString);
// users, acounts, sessions, verificationTokens, ...
// other options... 2. middleware.ts (Authentication Middleware - Edge Compatible) import authConfig from "../authConfig";
import NextAuth from "next-auth";
export const { auth: middleware } = NextAuth(authConfig); 💡 This ensures that only the Edge-compatible adapter logic is used in middleware. 3. auth.ts (Final NextAuth Setup with Adapter and Node.js Runtime) import NextAuth from "next-auth";
import authConfig from "../../authConfig";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { accounts, db, sessions, users, verificationTokens } from "../../db/schema";
import Nodemailer from "next-auth/providers/nodemailer";
export const { handlers, signIn, signOut, auth } = NextAuth({
...authConfig,
adapter: DrizzleAdapter(db, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
verificationTokensTable: verificationTokens,
}),
// other options...
}); This worked perfectly for my setup! |
Adapter type
@auth/drizzle-adapter
Environment
Reproduction URL
https://github.com/Maisaan/drizzle-adapter-error
Describe the issue
Getting the following errors when trying to access req.auth inside of middleware, when middleware file is deleted this works without errors. I want to setup basic auth redirects.
I have updated next-auth-example template with my code to recreate error
Instead of a middleware i am currently using an HOC to check if session exists and redirect to auth screen, this is not ideal but works for now.
How to reproduce
pnpm install
.env.local.example
npx drizzle-kit push
to update supabaseExpected behavior
Authenticate user and redirect to home screen
The text was updated successfully, but these errors were encountered: