-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Open
feat: NATS KV adapter #13172
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:" }), | ||
}) | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.