I’m using Auth.js (v5) as the authentication library in a Next.js application. I’m using Unstorage as a Database Adapter to store session data in a key-value storage. The storage works fine with In Memory storage, but that’s not suitable for production, so I’m switching to Redis storage. Unstorage uses ioredis under the hood to connect to Redis.
Following the steps in all the official docs, I encounter the following error after starting the application:
Cannot read properties of undefined (reading ‘charCodeAt’)
Digging into the code (./node_modules/ioredis/built/cluster/ClusterOptions.js:4:1), I found that the error is due the fact that ioredis uses dns (and other) modules specific to Node.
Module not found: Can’t resolve ‘dns’
Since the authentication runs on middleware, which runs on the Edge Runtime, these modules are unavailable. Next.js’s official docs suggest a workaround, but I don’t see how it applies to Auth.js and middleware.
My question is: Is there a way to use Redis to store session and user info in a Next.js app running Auth.js? Am I missing something?
Versions:
- “react”: “18”
- “next”: “14.2.4”
- “next-auth”: “5.0.0-beta.19”
- “unstorage”: “1.10.2”
- “ioredis”: “5.4.1”
Steps to reproduce:
- Execute the following commands:
npx create-next-app@latest
cd my-app
npm install next-auth@beta
npx auth secret
npm install unstorage @auth/unstorage-adapter ioredis
- Create the following files:
- ./auth.ts
import NextAuth from "next-auth"
import { UnstorageAdapter } from "@auth/unstorage-adapter"
import redisDriver from "unstorage/drivers/redis"
import { createStorage } from "unstorage"
const storage = createStorage({
driver: redisDriver({
host: 'localhost',
tls: false as any,
port: 6379
}),
});
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: UnstorageAdapter(storage),
providers: [],
})
./middleware.ts
export { auth as middleware } from "@/auth"
./app/api/auth/[…nextauth]/route.ts
import { handlers } from "@/auth"
export const { GET, POST } = handlers
- Execute the app
npx next dev