Upstash Redis アダプター
リソース
セットアップ
インストール
npm install @upstash/redis @auth/upstash-redis-adapter
環境変数
UPSTASH_REDIS_URL,
UPSTASH_REDIS_TOKEN
設定
./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: [],
})
高度な使用法
単一の Upstash Redis インスタンスで複数のアプリを使用する
Upstash の無料枠では、Redis インスタンスを 1 つしか使用できません。このインスタンスを使用する複数の Auth.js 接続アプリがある場合は、アプリごとに異なるキープレフィックスが必要です。
アダプターファクトリー関数の 2 番目の引数として options
オブジェクトを渡すことで、プレフィックスを変更できます。
このオブジェクトのデフォルト値は次のとおりです
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:",
}
通常、このシナリオでは baseKeyPrefix
を変更するだけで十分ですが、よりカスタムなセットアップでは、すべてのキーのプレフィックスを変更することもできます。
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: UpstashRedisAdapter(redis, { baseKeyPrefix: "app2:" }),
})