@auth/dynamodb-adapter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +28 -0
- package/index.d.ts +175 -0
- package/index.d.ts.map +1 -0
- package/index.js +508 -0
- package/jest-dynamodb-config.js +41 -0
- package/jest.config.js +13 -0
- package/package.json +60 -0
- package/src/index.ts +545 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2023, Balázs Orbán
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<br/>
|
|
3
|
+
<a href="https://authjs.dev" target="_blank">
|
|
4
|
+
<img height="64px" src="https://authjs.dev/img/logo/logo-sm.png" />
|
|
5
|
+
</a>
|
|
6
|
+
<a href="https://aws.amazon.com/dynamodb" target="_blank">
|
|
7
|
+
<img height="64px" src="https://authjs.dev/img/adapters/dynamodb.svg"/>
|
|
8
|
+
</a>
|
|
9
|
+
<h3 align="center"><b>DynamoDB Adapter</b> - NextAuth.js / Auth.js</a></h3>
|
|
10
|
+
<p align="center" style="align: center;">
|
|
11
|
+
<a href="https://npm.im/@auth/dynamodb-adapter">
|
|
12
|
+
<img src="https://img.shields.io/badge/TypeScript-blue?style=flat-square" alt="TypeScript" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://npm.im/@auth/dynamodb-adapter">
|
|
15
|
+
<img alt="npm" src="https://img.shields.io/npm/v/@auth/dynamodb-adapter?color=green&label=@auth/dynamodb-adapter&style=flat-square">
|
|
16
|
+
</a>
|
|
17
|
+
<a href="https://www.npmtrends.com/@auth/dynamodb-adapter">
|
|
18
|
+
<img src="https://img.shields.io/npm/dm/@auth/dynamodb-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://github.com/nextauthjs/next-auth/stargazers">
|
|
21
|
+
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="Github Stars" />
|
|
22
|
+
</a>
|
|
23
|
+
</p>
|
|
24
|
+
</p>
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
Check out the documentation at [authjs.dev](https://authjs.dev/reference/adapter/dynamodb).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
|
|
3
|
+
* <p style={{fontWeight: "normal"}}>Official <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">DynamoDB</a> adapter for Auth.js / NextAuth.js.</p>
|
|
4
|
+
* <a href="https://docs.aws.amazon.com/dynamodb/index.html">
|
|
5
|
+
* <img style={{display: "block"}} src="https://authjs.dev/img/adapters/dynamodb.png" width="48"/>
|
|
6
|
+
* </a>
|
|
7
|
+
* </div>
|
|
8
|
+
*
|
|
9
|
+
* ## Installation
|
|
10
|
+
*
|
|
11
|
+
* ```bash npm2yarn2pnpm
|
|
12
|
+
* npm install next-auth @auth/dynamodb-adapter
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module @auth/dynamodb-adapter
|
|
16
|
+
*/
|
|
17
|
+
import type { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
|
|
18
|
+
import type { Adapter } from "@auth/core/adapters";
|
|
19
|
+
export interface DynamoDBAdapterOptions {
|
|
20
|
+
tableName?: string;
|
|
21
|
+
partitionKey?: string;
|
|
22
|
+
sortKey?: string;
|
|
23
|
+
indexName?: string;
|
|
24
|
+
indexPartitionKey?: string;
|
|
25
|
+
indexSortKey?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* ## Setup
|
|
29
|
+
*
|
|
30
|
+
* By default, the adapter expects a table with a partition key `pk` and a sort key `sk`, as well as a global secondary index named `GSI1` with `GSI1PK` as partition key and `GSI1SK` as sorting key. To automatically delete sessions and verification requests after they expire using [dynamodb TTL](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) you should [enable the TTL](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html) with attribute name 'expires'. You can set whatever you want as the table name and the billing method.
|
|
31
|
+
* You can find the full schema in the table structure section below.
|
|
32
|
+
*
|
|
33
|
+
* ### Configuring Auth.js
|
|
34
|
+
*
|
|
35
|
+
* You need to pass `DynamoDBDocument` client from the modular [`aws-sdk`](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-example-dynamodb-utilities.html) v3 to the adapter.
|
|
36
|
+
* The default table name is `next-auth`, but you can customise that by passing `{ tableName: 'your-table-name' }` as the second parameter in the adapter.
|
|
37
|
+
*
|
|
38
|
+
* ```javascript title="pages/api/auth/[...nextauth].js"
|
|
39
|
+
* import { DynamoDB, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb"
|
|
40
|
+
* import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb"
|
|
41
|
+
* import NextAuth from "next-auth";
|
|
42
|
+
* import Providers from "next-auth/providers";
|
|
43
|
+
* import { DynamoDBAdapter } from "@auth/dynamodb-adapter"
|
|
44
|
+
*
|
|
45
|
+
* const config: DynamoDBClientConfig = {
|
|
46
|
+
* credentials: {
|
|
47
|
+
* accessKeyId: process.env.NEXT_AUTH_AWS_ACCESS_KEY as string,
|
|
48
|
+
* secretAccessKey: process.env.NEXT_AUTH_AWS_SECRET_KEY as string,
|
|
49
|
+
* },
|
|
50
|
+
* region: process.env.NEXT_AUTH_AWS_REGION,
|
|
51
|
+
* };
|
|
52
|
+
*
|
|
53
|
+
* const client = DynamoDBDocument.from(new DynamoDB(config), {
|
|
54
|
+
* marshallOptions: {
|
|
55
|
+
* convertEmptyValues: true,
|
|
56
|
+
* removeUndefinedValues: true,
|
|
57
|
+
* convertClassInstanceToMap: true,
|
|
58
|
+
* },
|
|
59
|
+
* })
|
|
60
|
+
*
|
|
61
|
+
* export default NextAuth({
|
|
62
|
+
* // Configure one or more authentication providers
|
|
63
|
+
* providers: [
|
|
64
|
+
* Providers.GitHub({
|
|
65
|
+
* clientId: process.env.GITHUB_ID,
|
|
66
|
+
* clientSecret: process.env.GITHUB_SECRET,
|
|
67
|
+
* }),
|
|
68
|
+
* Providers.Email({
|
|
69
|
+
* server: process.env.EMAIL_SERVER,
|
|
70
|
+
* from: process.env.EMAIL_FROM,
|
|
71
|
+
* }),
|
|
72
|
+
* // ...add more providers here
|
|
73
|
+
* ],
|
|
74
|
+
* adapter: DynamoDBAdapter(
|
|
75
|
+
* client
|
|
76
|
+
* ),
|
|
77
|
+
* ...
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* (AWS secrets start with `NEXT_AUTH_` in order to not conflict with [Vercel's reserved environment variables](https://vercel.com/docs/environment-variables#reserved-environment-variables).)
|
|
82
|
+
*
|
|
83
|
+
* ## Advanced usage
|
|
84
|
+
*
|
|
85
|
+
* ### Default schema
|
|
86
|
+
*
|
|
87
|
+
* The table respects the single table design pattern. This has many advantages:
|
|
88
|
+
*
|
|
89
|
+
* - Only one table to manage, monitor and provision.
|
|
90
|
+
* - Querying relations is faster than with multi-table schemas (for eg. retrieving all sessions for a user).
|
|
91
|
+
* - Only one table needs to be replicated if you want to go multi-region.
|
|
92
|
+
*
|
|
93
|
+
* > This schema is adapted for use in DynamoDB and based upon our main [schema](https://authjs.dev/reference/adapters#models)
|
|
94
|
+
*
|
|
95
|
+
* 
|
|
96
|
+
*
|
|
97
|
+
* You can create this table with infrastructure as code using [`aws-cdk`](https://github.com/aws/aws-cdk) with the following table definition:
|
|
98
|
+
*
|
|
99
|
+
* ```javascript title=stack.ts
|
|
100
|
+
* new dynamodb.Table(this, `NextAuthTable`, {
|
|
101
|
+
* tableName: "next-auth",
|
|
102
|
+
* partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
|
|
103
|
+
* sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
|
|
104
|
+
* timeToLiveAttribute: "expires",
|
|
105
|
+
* }).addGlobalSecondaryIndex({
|
|
106
|
+
* indexName: "GSI1",
|
|
107
|
+
* partitionKey: { name: "GSI1PK", type: dynamodb.AttributeType.STRING },
|
|
108
|
+
* sortKey: { name: "GSI1SK", type: dynamodb.AttributeType.STRING },
|
|
109
|
+
* })
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* Alternatively, you can use this cloudformation template:
|
|
113
|
+
*
|
|
114
|
+
* ```yaml title=cloudformation.yaml
|
|
115
|
+
* NextAuthTable:
|
|
116
|
+
* Type: "AWS::DynamoDB::Table"
|
|
117
|
+
* Properties:
|
|
118
|
+
* TableName: next-auth
|
|
119
|
+
* AttributeDefinitions:
|
|
120
|
+
* - AttributeName: pk
|
|
121
|
+
* AttributeType: S
|
|
122
|
+
* - AttributeName: sk
|
|
123
|
+
* AttributeType: S
|
|
124
|
+
* - AttributeName: GSI1PK
|
|
125
|
+
* AttributeType: S
|
|
126
|
+
* - AttributeName: GSI1SK
|
|
127
|
+
* AttributeType: S
|
|
128
|
+
* KeySchema:
|
|
129
|
+
* - AttributeName: pk
|
|
130
|
+
* KeyType: HASH
|
|
131
|
+
* - AttributeName: sk
|
|
132
|
+
* KeyType: RANGE
|
|
133
|
+
* GlobalSecondaryIndexes:
|
|
134
|
+
* - IndexName: GSI1
|
|
135
|
+
* Projection:
|
|
136
|
+
* ProjectionType: ALL
|
|
137
|
+
* KeySchema:
|
|
138
|
+
* - AttributeName: GSI1PK
|
|
139
|
+
* KeyType: HASH
|
|
140
|
+
* - AttributeName: GSI1SK
|
|
141
|
+
* KeyType: RANGE
|
|
142
|
+
* TimeToLiveSpecification:
|
|
143
|
+
* AttributeName: expires
|
|
144
|
+
* Enabled: true
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* ### Using a custom schema
|
|
148
|
+
*
|
|
149
|
+
* You can configure your custom table schema by passing the `options` key to the adapter constructor:
|
|
150
|
+
*
|
|
151
|
+
* ```javascript
|
|
152
|
+
* const adapter = DynamoDBAdapter(client, {
|
|
153
|
+
* tableName: "custom-table-name",
|
|
154
|
+
* partitionKey: "custom-pk",
|
|
155
|
+
* sortKey: "custom-sk",
|
|
156
|
+
* indexName: "custom-index-name",
|
|
157
|
+
* indexPartitionKey: "custom-index-pk",
|
|
158
|
+
* indexSortKey: "custom-index-sk",
|
|
159
|
+
* })
|
|
160
|
+
* ```
|
|
161
|
+
**/
|
|
162
|
+
export declare function DynamoDBAdapter(client: DynamoDBDocument, options?: DynamoDBAdapterOptions): Adapter;
|
|
163
|
+
declare const format: {
|
|
164
|
+
/** Takes a plain old JavaScript object and turns it into a Dynamodb object */
|
|
165
|
+
to(object: Record<string, any>): Record<string, unknown>;
|
|
166
|
+
/** Takes a Dynamo object and returns a plain old JavaScript object */
|
|
167
|
+
from<T = Record<string, unknown>>(object?: Record<string, any>): T | null;
|
|
168
|
+
};
|
|
169
|
+
declare function generateUpdateExpression(object: Record<string, any>): {
|
|
170
|
+
UpdateExpression: string;
|
|
171
|
+
ExpressionAttributeNames: Record<string, string>;
|
|
172
|
+
ExpressionAttributeValues: Record<string, unknown>;
|
|
173
|
+
};
|
|
174
|
+
export { format, generateUpdateExpression };
|
|
175
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAEV,gBAAgB,EACjB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EACV,OAAO,EAKR,MAAM,qBAAqB,CAAA;AAE5B,MAAM,WAAW,sBAAsB;IACrC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAsII;AACJ,wBAAgB,eAAe,CAC7B,MAAM,EAAE,gBAAgB,EACxB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CA0ST;AASD,QAAA,MAAM,MAAM;IACV,8EAA8E;eACnE,OAAO,MAAM,EAAE,GAAG,CAAC;IAY9B,sEAAsE;+CAC3B,OAAO,MAAM,EAAE,GAAG,CAAC;CAsB/D,CAAA;AAED,iBAAS,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;IAC9D,gBAAgB,EAAE,MAAM,CAAA;IACxB,wBAAwB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChD,yBAAyB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACnD,CAgBA;AAED,OAAO,EAAE,MAAM,EAAE,wBAAwB,EAAE,CAAA"}
|