Skip to content

Commit

Permalink
Add Upstash Redis Adapter docs (nextauthjs#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
kay-is authored Jan 17, 2022
1 parent fb39bcf commit 3417ccb
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/adapters/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ We have a list of official adapters that are distributed as their own packages u
- [`neo4j`](./neo4j)
- [`typeorm-legacy`](./typeorm)
- [`sequelize`](./sequelize)
- [`dgraph`](./dgraph)
- [`dgraph`](./dgraph)
- [`upstash-redis`](./upstash-redis)

## Custom Adapter

Expand Down
69 changes: 69 additions & 0 deletions docs/adapters/upstash-redis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
id: upstash-redis
title: Upstash Redis
---

# Upstash Redis

To use this Adapter, you need to install `@upstash/redis` and `@next-auth/upstash-redis-adapter` package:

```bash npm2yarn
npm install @upstash/redis @next-auth/upstash-redis-adapter
```

Configure your NextAuth.js to use the Upstash Redis Adapter:

```javascript title="pages/api/auth/[...nextauth].js"
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,
}),
],
})
```

## Using Multiple Apps with a Single Upstash Redis Instance

The Upstash free-tier allows for only one Redis instance. If you have multiple Next-Auth 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:

```js
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:

```js
export default NextAuth({
...
adapter: UpstashRedisAdapter(redis, {baseKeyPrefix: "app2:"})
...
})
```

0 comments on commit 3417ccb

Please sign in to comment.