Skip to content
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

fix(docs): Update D1 adapter configuration code #12164

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 22 additions & 26 deletions docs/pages/getting-started/adapters/d1.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,33 @@ import { Code } from "@/components/Code"
### Installation

```bash npm2yarn
npm install next-auth @auth/d1-adapter
npm install @auth/d1-adapter
```

### Environment Variables
### Bindings

Environment variables in Cloudflare's platform are set either via a [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables) configuration file, or in the [admin dashboard](https://dash.cloudflare.com/?to=/:account/pages/view/:pages-project/settings/environment-variables).
Bindings allow your Cloudflare Workers to interact with resources on the Cloudflare platform. To use this adapter, you must define the D1 binding in [`wrangler.toml`](https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases).

Follow [these docs](https://developers.cloudflare.com/d1/get-started/#4-bind-your-worker-to-your-d1-database) for instructions on how to do so.

### Configuration

<Code>
<Code.Next>

Once you have [set up next-on-pages](https://developers.cloudflare.com/pages/framework-guides/nextjs/ssr/get-started/), you can access bindings via `getRequestContext`.


```ts filename="./auth.ts"
import NextAuth from "next-auth"
import { D1Adapter } from "@auth/d1-adapter"
import { getRequestContext } from "@cloudflare/next-on-pages"

export const { handlers, auth, signIn, signOut } = NextAuth({
providers: [],
adapter: D1Adapter(env.db),
export const { handlers, auth, signIn, signOut } = NextAuth(() => {
return {
providers: [],
adapter: D1Adapter(getRequestContext().env.db),
}
})
```

Expand All @@ -44,9 +52,9 @@ import { QwikAuth$ } from "@auth/qwik"
import { D1Adapter } from "@auth/d1-adapter"

export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
() => ({
(event) => ({
providers: [],
adapter: D1Adapter(env.db),
adapter: D1Adapter(event.platform.env.db),
})
)
```
Expand All @@ -58,30 +66,18 @@ export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
import { SvelteKitAuth } from "@auth/sveltekit"
import { D1Adapter } from "@auth/d1-adapter"

export const { handle, signIn, signOut } = SvelteKitAuth({
providers: [],
adapter: D1Adapter(env.db),
export const { handle, signIn, signOut } = SvelteKitAuth(async (event) => {
return {
providers: [],
adapter: D1Adapter(event.platform.env.db),
}
})
```

</Code.Svelte>
<Code.Express>

```ts filename="./src/routes/auth.route.ts"
import { ExpressAuth } from "@auth/express"
import { D1Adapter } from "@auth/d1-adapter"

const app = express()

app.set("trust proxy", true)
app.use(
"/auth/*",
ExpressAuth({
providers: [],
adapter: D1Adapter(env.db),
})
)
```
Cloudflare Workers do not support the full Node.js API, and libraries that depend on Node.js modules (like express) may not work directly.

</Code.Express>
</Code>
Expand Down
Loading