diff --git a/pages/cloudflare/_meta.json b/pages/cloudflare/_meta.json index d0150a0..aff2153 100644 --- a/pages/cloudflare/_meta.json +++ b/pages/cloudflare/_meta.json @@ -3,6 +3,7 @@ "get-started": "", "bindings": "", "caching": "", + "howtos": "How-Tos", "examples": "", "community": "Community projects", "troubleshooting": "", diff --git a/pages/cloudflare/howtos/NextAuth.mdx b/pages/cloudflare/howtos/NextAuth.mdx new file mode 100644 index 0000000..7c7fa0f --- /dev/null +++ b/pages/cloudflare/howtos/NextAuth.mdx @@ -0,0 +1,31 @@ + +## [NextAuth.js](https://next-auth.js.org/) + +NextAuth.js is an open-source authentication solution for Next.js applications. + +### Solving a broken build + +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. + +However you can configure NextAuth.js to use custom implementations of the `encode` and `decode` functions that do not use the unimplemented Node APIs. Implementations built on top of [`SubtleCrypto`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto) can run on workerd. + +The NextAuth.js configuration file should look like: + +```js +import { encode, decode } from "@/lib/webcrypto"; + +export const NEXT_AUTH_CONFIG = { + // ... + jwt: { + encode, + decode, + }, +}; +``` + +Kudos to Arnav Gupta ([`@arnavgupta00`](https://github.com/arnavgupta00)) for coming up with the solution. +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).