NextAuth.js (opens in a new tab)
Starting from wrangler 4.4.0, the implementation of createCipheriv
is available and NextAuth.js should
work out of the box.
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
(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).
Solving issues in local dev
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 calling getCloudflareContext
in your NextAuth
callback, for example like so:
export const { handlers, auth, signIn, signOut } = NextAuth(async _ => {
let { env } = await getCloudflareContext({async: true})
..
}