Skip to content
Migrating from NextAuth.js v4? Read our migration guide.
Getting Started
Adapters
Upstash Redis

Upstash Redis Adapter

Resources

Setup

Installation

npm install @upstash/redis @auth/upstash-redis-adapter

Environment Variables

UPSTASH_REDIS_URL,
UPSTASH_REDIS_TOKEN

Configuration

./auth.ts
import NextAuth from "next-auth"
import { UpstashRedisAdapter } from "@auth/upstash-redis-adapter"
import { Redis } from "@upstash/redis"
 
const redis = new Redis({
  url: process.env.UPSTASH_REDIS_URL!,
  token: process.env.UPSTASH_REDIS_TOKEN!,
})
 
export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: UpstashRedisAdapter(redis),
  providers: [],
})

Advanced usage

Using multiple apps with a single Upstash Redis instance

The Upstash free-tier allows for only one Redis instance. If you have multiple Auth.js connected apps using this instance, you need different key prefixes for every app.

You can change the prefixes by passing an options object as the second argument to the adapter factory function.

The default values for this object are:

const defaultOptions = {
  baseKeyPrefix: "",
  accountKeyPrefix: "user:account:",
  accountByUserIdPrefix: "user:account:by-user-id:",
  emailKeyPrefix: "user:email:",
  sessionKeyPrefix: "user:session:",
  sessionByUserIdKeyPrefix: "user:session:by-user-id:",
  userKeyPrefix: "user:",
  verificationTokenKeyPrefix: "user:token:",
}

Usually changing the baseKeyPrefix should be enough for this scenario, but for more custom setups, you can also change the prefixes of every single key.

export const { handlers, auth, signIn, signOut } = NextAuth({
  adapter: UpstashRedisAdapter(redis, { baseKeyPrefix: "app2:" }),
})
Auth.js © Balázs Orbán and Team - 2024