@next-auth/upstash-redis-adapter
Official Upstash Redis adapter for Auth.js / NextAuth.js.
Installationβ
- npm
- yarn
- pnpm
npm install @upstash/redis @next-auth/upstash-redis-adapter
yarn add @upstash/redis @next-auth/upstash-redis-adapter
pnpm add @upstash/redis @next-auth/upstash-redis-adapter
UpstashRedisAdapter()β
Setupβ
Configure Auth.js to use the Upstash Redis Adapter:
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import { UpstashRedisAdapter } from "@next-auth/upstash-redis-adapter";
import upstashRedisClient from "@upstash/redis";
const redis = upstashRedisClient(
process.env.UPSTASH_REDIS_URL,
process.env.UPSTASH_REDIS_TOKEN
);
export default NextAuth({
adapter: UpstashRedisAdapter(redis),
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
});
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.
Example:
export default NextAuth({
...
adapter: UpstashRedisAdapter(redis, {baseKeyPrefix: "app2:"})
...
})
UpstashRedisAdapter(
client
:Redis
,options
:UpstashRedisAdapterOptions
={}
):Adapter
Parametersβ
Parameter | Type |
---|---|
client | Redis |
options | UpstashRedisAdapterOptions |
Returnsβ
Adapter
UpstashRedisAdapterOptionsβ
This is the interface of the Upstash Redis adapter options.
Propertiesβ
accountByUserIdPrefix?β
accountByUserIdPrefix:
string
The prefix for the accountByUserId
key
accountKeyPrefix?β
accountKeyPrefix:
string
The prefix for the account
key
baseKeyPrefix?β
baseKeyPrefix:
string
The base prefix for your keys
emailKeyPrefix?β
emailKeyPrefix:
string
The prefix for the emailKey
key
sessionByUserIdKeyPrefix?β
sessionByUserIdKeyPrefix:
string
The prefix for the sessionByUserId
key
sessionKeyPrefix?β
sessionKeyPrefix:
string
The prefix for the sessionKey
key
userKeyPrefix?β
userKeyPrefix:
string
The prefix for the user
key
verificationTokenKeyPrefix?β
verificationTokenKeyPrefix:
string
The prefix for the verificationToken
key