Skip to content

Commit 30877af

Browse files
Add HowTo NextAuth on cloudflare
Update pages/cloudflare/howtos/NextAuth.mdx Co-authored-by: Dario Piotrowicz <dario.piotrowicz@gmail.com>
1 parent 511a541 commit 30877af

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pages/cloudflare/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"get-started": "",
44
"bindings": "",
55
"caching": "",
6+
"howtos": "How-Tos",
67
"examples": "",
78
"community": "Community projects",
89
"troubleshooting": "",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
## [NextAuth.js](https://next-auth.js.org/)
3+
4+
NextAuth.js is an open-source authentication solution for Next.js applications.
5+
6+
### Solving a broken build
7+
8+
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).
9+
10+
`createCipheriv` is not currently implemented by the workerd runtime so apps using NextAuth.js with the default configuration break at build time.
11+
12+
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.
13+
14+
The NextAuth.js configuration file should look like:
15+
16+
```js
17+
import { encode, decode } from "@/lib/webcrypto";
18+
19+
export const NEXT_AUTH_CONFIG = {
20+
// ...
21+
jwt: {
22+
encode,
23+
decode,
24+
},
25+
};
26+
```
27+
28+
Kudos to Arnav Gupta ([`@arnavgupta00`](https://github.com/arnavgupta00)) for coming up with the solution.
29+
You can find an example of this on his [example repository](https://github.com/arnavgupta00/deployment-cf-workers-prisma-nextauth).
30+
31+
Related issues: [`workers-sdk#206`](https://github.com/opennextjs/opennextjs-cloudflare/issues/206) and [`workerd#3277`](https://github.com/cloudflare/workerd/issues/3277).

0 commit comments

Comments
 (0)