-
Notifications
You must be signed in to change notification settings - Fork 22
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
[BUG] Authjs middleware casues self._ENTRIES[b.name].default is not a function #252
Comments
Encountered the same issue. I conducted several experiments and finally found a solution. The root cause might not be related to Auth.js, but rather to the use of top-level I conducted several experiments. As long as you import code that uses top-level export async function test1() {
return new Promise(resolve => resolve("Hello"));
}
export const testAsync = await test1(); SolutionTo resolve the issue, we should avoid importing any code that uses top-level Here is a working demo: // middleware.ts
import { NextResponse } from "next/server";
import { getDB } from "@/lib/db";
import NextAuth from "next-auth";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { accounts, sessions, users, verificationTokens } from "@/db/schema";
import Google from "next-auth/providers/google";
import { env } from "@/env.mjs";
export default async function middleware(request) {
console.log("middleware is running!");
const db = await getDB();
const { auth } = initAuthJs(db);
const response = auth(request);
if (response) {
return response;
}
return NextResponse.next();
}
export function initAuthJs(drizzleDBInstance) {
return NextAuth({
trustHost: true,
adapter: DrizzleAdapter(drizzleDBInstance, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
verificationTokensTable: verificationTokens,
}),
providers: [
Google({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
})
]
});
} Some explanations:
|
Thanks for figuring out the root cause @willenigma, great investigation! We will look into that. Note that anyway, the following code from @mackenly : const authResult = async (): Promise<NextAuthResult> => {
return NextAuth({
providers: [
Resend({
apiKey: (await getCloudflareContext()).env.AUTH_RESEND_KEY,
from: (await getCloudflareContext()).env.AUTH_EMAIL_FROM,
}),
],
adapter: D1Adapter((await getCloudflareContext()).env.DB),
});
};
export const { handlers, signIn, signOut, auth } = await authResult(); has problem because So lazy init is the right thing to do here. (We plan to improve the DX here: #229) |
Hi there 🙂 , thanks for the issue @mackenly and thanks a lot for the exploration @willenigma! That's been super helpful! 🫶 #268 should address the issue, please try the PR's prerelease and let me know if it works for you 😄🙏 |
We merged the PR that closed this issue, @mackenly, @willenigma if you still experience the issue please let me know and we can reopen this 🙂 |
Working great for me. Thanks! 🎉 |
Describe the bug
When Authjs' middleware is used (see below), the following error is thrown:
Authjs middleware.ts (from here):
If I use an example middleware like this one, everything works as expected. It's only with Authjs that it seems to break.
Steps to reproduce
Here's a repo with the code: https://github.com/mackenly/temp-auth-js-demo
Minimal example with Authjs installed.
Expected behavior
No exceptions to be thrown.
@opennextjs/cloudflare version
0.3.8
Wrangler version
3.101.0
next info output
Additional context
No response
The text was updated successfully, but these errors were encountered: