Cloudflare
How-Tos
NextAuth

NextAuth.js (opens in a new tab)

NextAuth.js is an open-source authentication solution for Next.js applications.

Solving a broken build

NextAuth.js relies on createCipheriv (opens in a new tab) from node:crypto (opens in a new tab).

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 (opens in a new tab) can run on workerd.

The NextAuth.js configuration file should look like:

import { encode, decode } from "@/lib/webcrypto";
 
export const NEXT_AUTH_CONFIG = {
  // ...
  jwt: {
    encode,
    decode,
  },
};

Kudos to Arnav Gupta (@arnavgupta00 (opens in a new tab)) for coming up with the solution. You can find an example of this on his example repository (opens in a new tab).

Related issues: workers-sdk#206 (opens in a new tab) and workerd#3277 (opens in a new tab).