From bfea880dd5c3662376029cf17125e7f49621bf36 Mon Sep 17 00:00:00 2001 From: Mark Dembo Date: Thu, 6 Mar 2025 11:21:10 +0100 Subject: [PATCH 1/4] [cloudflare] Add nextauth infos --- pages/cloudflare/howtos/NextAuth.mdx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pages/cloudflare/howtos/NextAuth.mdx b/pages/cloudflare/howtos/NextAuth.mdx index a8e1066..89ed09a 100644 --- a/pages/cloudflare/howtos/NextAuth.mdx +++ b/pages/cloudflare/howtos/NextAuth.mdx @@ -4,6 +4,11 @@ NextAuth.js is an open-source authentication solution for Next.js applications. ### Solving a broken build + +This section offers a solution for NextAuth.js v4. Use of v5 is currently blocked by the lack of `createCipheriv` implementation. + + + NextAuth.js relies on [`createCipheriv`](https://nodejs.org/docs/v22.13.1/api/crypto.html#cryptocreatecipherivalgorithm-key-iv-options) from [`node:crypto`](https://nodejs.org/docs/v22.13.1/api/crypto.html). `createCipheriv` is not currently implemented by the workerd runtime so apps using NextAuth.js with the default configuration break at build time. @@ -28,3 +33,18 @@ Kudos to Arnav Gupta ([`@arnavgupta00`](https://github.com/arnavgupta00)) for co You can find an example of this on his [example repository](https://github.com/arnavgupta00/deployment-cf-workers-prisma-nextauth). Related issues: [`workers-sdk#206`](https://github.com/opennextjs/opennextjs-cloudflare/issues/206) and [`workerd#3277`](https://github.com/cloudflare/workerd/issues/3277). + +### Solving issues in local dev + +When trying to access bindings (for instance to use [D1](/d1/) as database) depending on your implementation, you might run into: + +`ERROR: getCloudflareContext has been called without having called initOpenNextCloudflareForDev from the Next.js config file.` + +You can resolve this issue, by following the the following pattern in your `auth.ts` configuration: + +``` +export const { handlers, auth, signIn, signOut } = NextAuth( async _ => { + let { env } = await getCloudflareContext({async: true}) + .. +} +``` \ No newline at end of file From 27cf5e515c63c9877ac150f474746a3e20bdafce Mon Sep 17 00:00:00 2001 From: Mark Dembo Date: Thu, 6 Mar 2025 12:12:01 +0100 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: Dario Piotrowicz --- pages/cloudflare/howtos/NextAuth.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pages/cloudflare/howtos/NextAuth.mdx b/pages/cloudflare/howtos/NextAuth.mdx index 89ed09a..2294ea1 100644 --- a/pages/cloudflare/howtos/NextAuth.mdx +++ b/pages/cloudflare/howtos/NextAuth.mdx @@ -36,14 +36,14 @@ Related issues: [`workers-sdk#206`](https://github.com/opennextjs/opennextjs-clo ### Solving issues in local dev -When trying to access bindings (for instance to use [D1](/d1/) as database) depending on your implementation, you might run into: +When trying to access Cloudflare bindings depending on your implementation, you might run into: `ERROR: getCloudflareContext has been called without having called initOpenNextCloudflareForDev from the Next.js config file.` -You can resolve this issue, by following the the following pattern in your `auth.ts` configuration: +You can resolve this issue, by calling `getCloudflareContext` in your `NextAuth` callback, for example like so: ``` -export const { handlers, auth, signIn, signOut } = NextAuth( async _ => { +export const { handlers, auth, signIn, signOut } = NextAuth(async _ => { let { env } = await getCloudflareContext({async: true}) .. } From 07fb925cdea698e8bb5be0ac2cdd5e2fba2cc6e5 Mon Sep 17 00:00:00 2001 From: Mark Dembo Date: Thu, 6 Mar 2025 12:18:40 +0100 Subject: [PATCH 3/4] Fix missing import and formatting --- pages/cloudflare/howtos/NextAuth.mdx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pages/cloudflare/howtos/NextAuth.mdx b/pages/cloudflare/howtos/NextAuth.mdx index 2294ea1..9ee83c2 100644 --- a/pages/cloudflare/howtos/NextAuth.mdx +++ b/pages/cloudflare/howtos/NextAuth.mdx @@ -1,3 +1,5 @@ +import { Callout } from "nextra/components"; + ## [NextAuth.js](https://next-auth.js.org/) NextAuth.js is an open-source authentication solution for Next.js applications. @@ -5,10 +7,10 @@ NextAuth.js is an open-source authentication solution for Next.js applications. ### Solving a broken build -This section offers a solution for NextAuth.js v4. Use of v5 is currently blocked by the lack of `createCipheriv` implementation. + This section offers a solution for NextAuth.js v4. Use of v5 is currently blocked by the lack of + `createCipheriv` implementation. - NextAuth.js relies on [`createCipheriv`](https://nodejs.org/docs/v22.13.1/api/crypto.html#cryptocreatecipherivalgorithm-key-iv-options) from [`node:crypto`](https://nodejs.org/docs/v22.13.1/api/crypto.html). `createCipheriv` is not currently implemented by the workerd runtime so apps using NextAuth.js with the default configuration break at build time. @@ -47,4 +49,4 @@ export const { handlers, auth, signIn, signOut } = NextAuth(async _ => { let { env } = await getCloudflareContext({async: true}) .. } -``` \ No newline at end of file +``` From b04ee5e35e4f9ab0d4379276708553b33880a2e6 Mon Sep 17 00:00:00 2001 From: Mark Dembo Date: Thu, 6 Mar 2025 13:20:58 +0100 Subject: [PATCH 4/4] Update pages/cloudflare/howtos/NextAuth.mdx Co-authored-by: Dario Piotrowicz --- pages/cloudflare/howtos/NextAuth.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/cloudflare/howtos/NextAuth.mdx b/pages/cloudflare/howtos/NextAuth.mdx index 9ee83c2..77b644e 100644 --- a/pages/cloudflare/howtos/NextAuth.mdx +++ b/pages/cloudflare/howtos/NextAuth.mdx @@ -44,7 +44,7 @@ When trying to access Cloudflare bindings depending on your implementation, you You can resolve this issue, by calling `getCloudflareContext` in your `NextAuth` callback, for example like so: -``` +```js export const { handlers, auth, signIn, signOut } = NextAuth(async _ => { let { env } = await getCloudflareContext({async: true}) ..