Open tianlelyd opened 1 month ago
I am getting a similar error, but is logs TypeError: Cannot read properties of undefined (reading 'prepare')
In my case it was a missing binding to D1 database on wrangler.toml
I am getting a similar error, but is logs
TypeError: Cannot read properties of undefined (reading 'prepare')
我收到类似的错误,但这是日志TypeError: Cannot read properties of undefined (reading 'prepare')
You're using the D1 adapter in auth, but you're passing in a drizzle object, so you'll get the hint you said
Solution
Using "next-auth": "^5.0.0-beta.19"
auth.ts
setup, add the Drizzle adapter// auth.ts
import NextAuth from "next-auth"
import { DrizzleAdapter } from "@auth/drizzle-adapter"
import { drizzle } from "drizzle-orm/d1"
import { getRequestContext } from "@cloudflare/next-on-pages"
import { accounts, sessions, users, verificationTokens } from "./schema"
import GitHub from "next-auth/providers/github"
export const { handlers, auth, signIn, signOut } = NextAuth(() => {
const db = drizzle(getRequestContext().env.DB)
return {
adapter: DrizzleAdapter(db, {
usersTable: users,
accountsTable: accounts,
sessionsTable: sessions,
verificationTokensTable: verificationTokens,
}),
session: { strategy: "jwt" },
providers: [GitHub]
})
And then the [...nextauth]/route.ts
// [...nextauth]/route.ts
import { handlers } from "@/auth" // Referring to the auth.ts we just created
export const { GET, POST } = handlers
export const runtime = "edge" // required for cloudflare
You should be able to interact with the d1 database now (server-side)
Important There still seems to be inconsistent support for edge with this approach, but you should be able to work with D1 & drizzle. I just threw this together, may have some issues, but should have a minimal example of a functioning drizzle + d1 setup https://github.com/eli-front/next-auth-cloudflare-d1-drizzle.
In my case it was a missing binding to D1 database on
wrangler.toml
How you had fixed this
export const db = drizzle(process.env.DB, { schema, logger: true })
export const db = drizzle(process.env.DB, { schema, logger: true })
Make sure you have the DB binding in your wrangler.toml
[[d1_databases]]
binding = "DB"
database_name = "your-database-name"
[[d1_databases]] binding = "DB" database_name = "cfd1"
its already done
these seem to be related: https://github.com/cloudflare/next-on-pages/issues/683
Adapter type
@auth/drizzle-adapter
Environment
Reproduction URL
https://github.com/tianlelyd/drizzle-adapter-d1-test.git
Describe the issue
When using nextjs+drizzle+auth.js+adapter-drizzle, local testing works fine, but when compiling with
pnpx @cloudflare/next-on-pages@1
, an error occurs indicating that "getRequestContext is being called at the top level of a route file, this is not supported". However, I am not actually calling getRequestContext() at the top level.How to reproduce
run
pnpm run pages:build
Expected behavior
It should not prompt「getRequestContext` is being called at the top level of a route file, this is not supported」