Skip to content

Commit

Permalink
add Upstash Redis Session adapter (#699)
Browse files Browse the repository at this point in the history
Co-authored-by: pilcrowOnPaper <[email protected]>
  • Loading branch information
schweden1997 and pilcrowOnPaper committed Jul 5, 2023
1 parent 26f5240 commit e6cb093
Show file tree
Hide file tree
Showing 19 changed files with 285 additions and 32 deletions.
8 changes: 8 additions & 0 deletions .auri/$3s4lqgi3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
package: "@lucia-auth/adapter-session-redis" # package name
type: "minor" # "major", "minor", "patch"
---


Add Redis adapter for Upstash

Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ const auth = lucia({
- [libSQL](/database-adapters/libSQL): libSQL (Turso)
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ const auth = lucia({
- [Mongoose](/database-adapters/mongoose): MongoDB
- [`mysql2`](/database-adapters/mysql2): MySQL
- [`pg`](/database-adapters/pg): PostgreSQL
- [`postgres`](https://github.com/porsager/postgres): PostgreSQL
- [`postgres`](/database-adapters/postgres): PostgreSQL
- [Prisma](/database-adapters/prisma): MongoDB, MySQL, PostgreSQL, SQLite
- [Redis](/database-adapters/redis): Redis

### Provider specific adapters

- [Cloudflare D1](/database-adapters/cloudflare-d1)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Upstash](/database-adapters/upstash)

## Set up types

Expand Down
3 changes: 2 additions & 1 deletion documentation-v2/content/main/1.basics/0.database.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ We currently provide the following adapters:
- [Mongoose](/database-adapters/mongoose)
- [`mysql2`](/database-adapters/mysql2)
- [`pg`](/database-adapters/pg)
- [`postgres`](https://github.com/porsager/postgres)
- [`postgres`](/database-adapters/postgres)
- [PlanetScale serverless](/database-adapters/planetscale-serverless)
- [Prisma](/database-adapters/prisma)
- [Redis](/database-adapters/redis)
- [Upstash](/database-adapters/upstash)

You can also use query builders like Drizzle ORM and Kysely since they rely on underlying drivers that we provide adapters for.

Expand Down
52 changes: 52 additions & 0 deletions documentation-v2/content/main/2.database-adapters/upstash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
order: 0
title: "Upstash"
description: "Learn how to use Upstas Redis with Lucia"
---

Session adapter for [Upstash Redis](https://upstash.com) provided by the Redis session adapter package. This only handles sessions, and not users or keys.

```ts
import { upstash } from "@lucia-auth/adapter-session-redis";
```

```ts
const upstash: (
upstashClient: Redis,
prefixes?: {
session: string;
userSessions: string;
}
) => InitializeAdapter<SessionAdapter>;
```

##### Parameters

| name | type | optional | description |
| --------------- | ------------------------ | :------: | ------------------------------------ |
| `upstashClient` | `Redis` | | Serverless redis client for upstash. |
| `prefixes` | `Record<string, string>` || Key prefixes |

### Key prefixes

Key are defined as a combination of a prefix and an id so everything can be stored in a single Redis instance. By default, sessions are stored as `session:<session_id>` and user-sessions relationships are stored as `user_sessions:<user_id>`.

## Usage

```ts
import { lucia } from "lucia";
import { upstash } from "@lucia-auth/adapter-session-redis";
import { Redis } from "@upstash/redis";

const upstashClient = new Redis({
// ...
});

const auth = lucia({
adapter: {
user: userAdapter, // any normal adapter for storing users/keys
session: upstash(upstashClient)
}
// ...
});
```
7 changes: 6 additions & 1 deletion packages/adapter-session-redis/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
REDIS_PORT=""
# for Redis adapter
REDIS_PORT="your_redis_port"

# for Upstash adapter
URL="your_upstash_url"
TOKEN="your_upstash_token"
25 changes: 18 additions & 7 deletions packages/adapter-session-redis/README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
# `@lucia-auth/adapter-session-redis`

[Redis](https://redis.io) session adapter for Lucia

**[Documentation](https://lucia-auth.com/database/redis)**
Redis session adapters for Lucia

**[Lucia documentation](https://lucia-auth.com)**

**[Changelog](https://github.com/pilcrowOnPaper/lucia/blob/main/packages/session-adapter-redis/CHANGELOG.md)**

## Included adapters

- [Redis](https://redis.io) ([Documentation](https://v2.lucia-auth.com/database-adapters/redis))
- [Upstash](https://upstash.com) ([Documentation](https://v2.lucia-auth.com/database-adapters/upstash))

## Installation

```
npm install @lucia-auth/adapter-session-redis
```

`[email protected]` recommended.

## Testing

Add a postgresql database url to `.env`.
### Redis

Add the port of a local Redis DB to `.env`.

```
npm run test.redis
```

### Upstash

Add the `UPSTASH_REDIS_REST_URL` and the `UPSTASH_REDIS_REST_TOKEN` to `.env`.

```
npm run test
npm run test.upstash
```
28 changes: 21 additions & 7 deletions packages/adapter-session-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"type": "module",
"files": [
"/dist/",
"CHANGELOG.md"
"CHANGELOG.md",
"README.md"
],
"scripts": {
"build": "shx rm -rf ./dist/* && tsc",
"test": "tsx test/index.ts",
"test": "pnpm test.redis && pnpm test.upstash",
"test.redis": "tsx test/redis.ts",
"test.upstash": "tsx test/upstash.ts",
"auri.build": "pnpm build"
},
"keywords": [
Expand All @@ -22,7 +25,8 @@
"authentication",
"adapter",
"redis",
"session"
"session",
"upstash"
],
"repository": {
"type": "git",
Expand All @@ -36,13 +40,23 @@
},
"peerDependencies": {
"lucia": "2.0.0-beta.4",
"redis": "^4.0.0"
"redis": "^4.0.0",
"@upstash/redis": "^1.20.0"
},
"peerDependenciesMeta": {
"redis": {
"optional": true
},
"@upstash/redis": {
"optional": true
}
},
"devDependencies": {
"@lucia-auth/adapter-test": "latest",
"@lucia-auth/adapter-test": "workspace:*",
"@upstash/redis": "^1.21.0",
"dotenv": "^16.0.3",
"lucia": "workspace:*",
"redis": "^4.3.1",
"tsx": "^3.12.6",
"lucia": "latest"
"tsx": "^3.12.6"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SessionSchema, SessionAdapter, InitializeAdapter } from "lucia";
import type { InitializeAdapter, SessionAdapter, SessionSchema } from "lucia";
import type { RedisClientType } from "redis";

export const DEFAULT_SESSION_PREFIX = "session";
Expand Down
Loading

0 comments on commit e6cb093

Please sign in to comment.