Persist Auth.js data in Upstash Redis
An Auth.js application needs database-backed users, accounts, sessions, or verification tokens in Upstash Redis.
Auth.js · Const
UpstashRedisAdapter connects Auth.js to Upstash Redis for adapter-backed persistence. It accepts a Redis client and optional key-prefix settings, then returns an Auth.js Adapter.
UpstashRedisAdapter accepts an Upstash Redis client and optionally a prefix-options object, then returns an Auth.js Adapter. Auth.js uses that adapter to persist authentication data in Upstash Redis; custom prefixes can separate multiple applications sharing one Redis instance.
function UpstashRedisAdapter(client, options): Adapter| Name | Type | Description |
|---|---|---|
client | Redis | Upstash Redis client used by the adapter. |
options | UpstashRedisAdapterOptions | Optional key-prefix configuration. |
Adapter
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 } = NextAuth({
adapter: UpstashRedisAdapter(redis),
providers: [],
})Creates an Upstash Redis client and supplies it to Auth.js through the adapter factory.
An Auth.js application needs database-backed users, accounts, sessions, or verification tokens in Upstash Redis.
Several Auth.js applications use one Upstash Redis instance.
An application needs predictable Redis key organization.
UpstashRedisAdapter is a factory that takes a Redis client and returns an Auth.js Adapter. The returned adapter connects Auth.js persistence operations to Upstash Redis.
Follow-up: How would you isolate two applications sharing Redis? Pass different prefix settings in the second argument for each adapter instance.
The first argument is the Upstash Redis client, and the optional second argument contains UpstashRedisAdapterOptions. The function returns an Adapter.
Follow-up: Which option establishes a namespace for all keys? Use baseKeyPrefix, optionally combined with more specific key prefixes.
The adapter page documents prefixes for accounts, account lookups by user, email, sessions, sessions by user, users, and verification tokens. These prefixes control how related records are named in Redis.
Follow-up: Why are prefixes operationally useful? They prevent key collisions when applications share the same Redis instance.
UpstashRedisAdapterOptionsAdapterRedis