Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: NATS KV adapter #13172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lfbergee wants to merge 4 commits into nextauthjs:main
base: main
Choose a base branch
Loading
from lfbergee:main
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/pr-labeler.yml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hasura: ["packages/adapter-hasura/**/*"]
frameworks: ["packages/frameworks-*/**/*"]
mikro-orm: ["packages/adapter-mikro-orm/**/*"]
mongodb: ["packages/adapter-mongodb/**/*"]
nats-kv: ["packages/adapter-nats-kv/**/*"]
neo4j: ["packages/adapter-neo4j/**/*"]
next-auth: ["packages/next-auth/**/*"]
pg: ["packages/adapter-pg/**/*"]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ on:
- "@auth/kysely-adapter"
- "@auth/mikro-orm-adapter"
- "@auth/mongodb-adapter"
- "@auth/nats-kv-adapter"
- "@auth/neo4j-adapter"
- "@auth/pg-adapter"
- "@auth/pouchdb-adapter"
Expand Down
1 change: 1 addition & 0 deletions docs/pages/getting-started/adapters/_meta.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default {
kysely: "Kysely",
"mikro-orm": "MikroORM",
mongodb: "MongoDB",
nats: "NATS KV",
neo4j: "Neo4j",
neon: "Neon",
pg: "PostgreSQL",
Expand Down
319 changes: 319 additions & 0 deletions docs/pages/getting-started/adapters/nats.mdx
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
import { Code } from "@/components/Code"

<img align="right" src="/img/adapters/nats.svg" width="64" height="64" />

# NATS KV Adapter

## Resources

- [NATS documentation](https://docs.nats.io/)

## Setup

### Installation

```bash npm2yarn
npm install @nats-io/transport-node @nats-io/kv @auth/nats-kv-adapter
```

### Environment Variables

```sh
NATS_SERVERS,
NATS_CREDS
```

### Configuration

You can either use this with Symbol.asyncDispose or handle the disposal yourself.

#### With explicit resource management

If you do choose asyncDispose, make sure you environment is configured to handled that by targeting at least es2022 and the `lib` option to include `esnext` or `esnext.disposable`, or by providing a polyfill. Using this pattern the adapter will call the cleanup function when the adapter is after NATS operations. [https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html#using-declarations-and-explicit-resource-management)

<Code>
<Code.Next>

```ts filename="./auth.ts"
import NextAuth from "next-auth"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

async function getNats(): Promise<
{ kv: KV } & {
[Symbol.asyncDispose]: () => Promise<void>
}
> {
const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

return {
kv: kv,
[Symbol.asyncDispose]: async () => {
await nc.drain()
await nc.close()
},
}
}

export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: NatsKVAdapter(getNats),
providers: [],
})
```

</Code.Next>
<Code.Qwik>

```ts filename="/src/routes/plugin@auth.ts"
import { QwikAuth$ } from "@auth/qwik"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

async function getNats(): Promise<
{ kv: KV } & {
[Symbol.asyncDispose]: () => Promise<void>
}
> {
const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

return {
kv: kv,
[Symbol.asyncDispose]: async () => {
await nc.drain()
await nc.close()
},
}
}

export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
() => ({
providers: [],
adapter: NatsKVAdapter(getNats),
})
)
```

</Code.Qwik>
<Code.Svelte>

```ts filename="./src/auth.ts"
import { SvelteKitAuth } from "@auth/sveltekit"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

async function getNats(): Promise<
{ kv: KV } & {
[Symbol.asyncDispose]: () => Promise<void>
}
> {
const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

return {
kv: kv,
[Symbol.asyncDispose]: async () => {
await nc.drain()
await nc.close()
},
}
}

export const { handle, signIn, signOut } = SvelteKitAuth({
adapter: NatsKVAdapter(getNats),
providers: [],
})
```

</Code.Svelte>
<Code.Express>

```ts filename="./src/routes/auth.route.ts"
import { ExpressAuth } from "@auth/express"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

async function getNats(): Promise<
{ kv: KV } & {
[Symbol.asyncDispose]: () => Promise<void>
}
> {
const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

return {
kv: kv,
[Symbol.asyncDispose]: async () => {
await nc.drain()
await nc.close()
},
}
}

const app = express()

app.set("trust proxy", true)
app.use(
"/auth/*",
ExpressAuth({
providers: [],
adapter: NatsKVAdapter(getNats),
})
)
```

</Code.Express>
</Code>

#### Without explicit resource management

You can instead provide the adapter with a KV instance, and handle the connection and disposal yourself. Useful if you want to keep the connection alive, or have explicit control over the connection.

<Code>
<Code.Next>

```ts filename="./auth.ts"
import NextAuth from "next-auth"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: NatsKVAdapter(kv),
providers: [],
})
```

</Code.Next>
<Code.Qwik>

```ts filename="/src/routes/plugin@auth.ts"
import { QwikAuth$ } from "@auth/qwik"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

export const { onRequest, useSession, useSignIn, useSignOut } = QwikAuth$(
() => ({
providers: [],
adapter: NatsKVAdapter(kv),
})
)
```

</Code.Qwik>
<Code.Svelte>

```ts filename="./src/auth.ts"
import { SvelteKitAuth } from "@auth/sveltekit"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

export const { handle, signIn, signOut } = SvelteKitAuth({
adapter: NatsKVAdapter(kv),
providers: [],
})
```

</Code.Svelte>
<Code.Express>

```ts filename="./src/routes/auth.route.ts"
import { ExpressAuth } from "@auth/express"
import { NatsKVAdapter } from "@auth/nats-kv-adapter"
import { connect } from "@nats-io/transport-node"
import { Kvm, KV } from "@nats-io/kv"

const nc = await connect({
servers: process.env.NATS_SERVERS,
authenticator: process.env.NATS_CREDS,
})
const kvm = new Kvm(nc)
const kv = await kvm.create("name-of-auth-bucket")

const app = express()

app.set("trust proxy", true)
app.use(
"/auth/*",
ExpressAuth({
providers: [],
adapter: NatsKVAdapter(kv),
})
)
```

</Code.Express>
</Code>

### Advanced usage

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:

```ts
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.

```ts
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: NatsKVAdapter(kv, { baseKeyPrefix: "app2:" }),
})
```
1 change: 1 addition & 0 deletions docs/public/img/adapters/nats.svg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
28 changes: 28 additions & 0 deletions packages/adapter-nats-kv/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<p align="center">
<br/>
<a href="https://authjs.dev" target="_blank">
<img height="64px" src="https://authjs.dev/img/logo-sm.png" />
</a>
<a href="https://nats.io" target="_blank">
<img height="64px" src="https://authjs.dev/img/adapters/nats.svg"/>
</a>
<h3 align="center"><b>NATS KeyValue-store Adapter</b> - NextAuth.js / Auth.js</a></h3>
<p align="center" style="align: center;">
<a href="https://npm.im/@auth/nats-kv-adapter">
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
</a>
<a href="https://npm.im/@auth/nats-kv-adapter">
<img alt="npm" src="https://img.shields.io/npm/v/@auth/nats-kv-adapter?color=green&label=@auth/nats-kv-adapter&style=flat-square">
</a>
<a href="https://www.npmtrends.com/@auth/nats-kv-adapter">
<img src="https://img.shields.io/npm/dm/@auth/nats-kv-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
</a>
<a href="https://github.com/nextauthjs/next-auth/stargazers">
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="GitHub Stars" />
</a>
</p>
</p>

---

Check out the documentation at [authjs.dev](https://authjs.dev/reference/adapter/nats-kv).
6 changes: 6 additions & 0 deletions packages/adapter-nats-kv/docker-compose.yml
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
nats:
image: nats:latest
command: -js -p 5222
ports:
- "5222:5222"
Loading

AltStyle によって変換されたページ (->オリジナル) /