@auth/dynamodb-adapter 1.6.0 → 2.1.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/README.md +2 -2
- package/index.d.ts +3 -180
- package/index.d.ts.map +1 -1
- package/index.js +3 -180
- package/package.json +2 -2
- package/src/index.ts +3 -180
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<br/>
|
|
3
3
|
<a href="https://authjs.dev" target="_blank">
|
|
4
|
-
<img height="64px" src="https://authjs.dev/img/logo
|
|
4
|
+
<img height="64px" src="https://authjs.dev/img/logo-sm.png" />
|
|
5
5
|
</a>
|
|
6
6
|
<a href="https://aws.amazon.com/dynamodb" target="_blank">
|
|
7
7
|
<img height="64px" src="https://authjs.dev/img/adapters/dynamodb.svg"/>
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<img src="https://img.shields.io/npm/dm/@auth/dynamodb-adapter?label=%20downloads&style=flat-square" alt="Downloads" />
|
|
19
19
|
</a>
|
|
20
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="
|
|
21
|
+
<img src="https://img.shields.io/github/stars/nextauthjs/next-auth?style=flat-square" alt="GitHub Stars" />
|
|
22
22
|
</a>
|
|
23
23
|
</p>
|
|
24
24
|
</p>
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding:
|
|
3
|
-
* <p
|
|
2
|
+
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: "16px"}}>
|
|
3
|
+
* <p>Official <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">DynamoDB</a> adapter for Auth.js / NextAuth.js.</p>
|
|
4
4
|
* <a href="https://docs.aws.amazon.com/dynamodb/index.html">
|
|
5
5
|
* <img style={{display: "block"}} src="https://authjs.dev/img/adapters/dynamodb.png" width="48"/>
|
|
6
6
|
* </a>
|
|
@@ -24,186 +24,9 @@ export interface DynamoDBAdapterOptions {
|
|
|
24
24
|
indexPartitionKey?: string;
|
|
25
25
|
indexSortKey?: string;
|
|
26
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,
|
|
48
|
-
* secretAccessKey: process.env.NEXT_AUTH_AWS_SECRET_KEY,
|
|
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
|
-
* ## AWS Credentials
|
|
84
|
-
*
|
|
85
|
-
* :::note
|
|
86
|
-
* Always follow the **principle of least privilege** when giving access to AWS
|
|
87
|
-
* services/resources -> identities should only be permitted to perform the
|
|
88
|
-
* smallest set of actions necessary to fulfill a specific task.
|
|
89
|
-
* :::
|
|
90
|
-
*
|
|
91
|
-
* 1. Open the [AWS console](https://console.aws.amazon.com/) and go to "IAM", then "Users".
|
|
92
|
-
* 2. Create a new user. The purpose of this user is to give programmatic access to DynamoDB.
|
|
93
|
-
* 3. Create an Access Key and then copy Key ID and Secret to your `.env`/`.env.local` file.
|
|
94
|
-
* 4. Select "Add Permission" and "Create Inline Policy".
|
|
95
|
-
* 5. Copy the JSON below into the JSON input and replace `region`, `account_id` and `table_name` with your values.
|
|
96
|
-
*
|
|
97
|
-
* ```json
|
|
98
|
-
* {
|
|
99
|
-
* "Version": "2012-10-17",
|
|
100
|
-
* "Statement": [
|
|
101
|
-
* {
|
|
102
|
-
* "Sid": "DynamoDBAccess",
|
|
103
|
-
* "Effect": "Allow",
|
|
104
|
-
* "Action": [
|
|
105
|
-
* "dynamodb:BatchGetItem",
|
|
106
|
-
* "dynamodb:BatchWriteItem",
|
|
107
|
-
* "dynamodb:Describe*",
|
|
108
|
-
* "dynamodb:List*",
|
|
109
|
-
* "dynamodb:PutItem",
|
|
110
|
-
* "dynamodb:DeleteItem",
|
|
111
|
-
* "dynamodb:GetItem",
|
|
112
|
-
* "dynamodb:Scan",
|
|
113
|
-
* "dynamodb:Query",
|
|
114
|
-
* "dynamodb:UpdateItem"
|
|
115
|
-
* ],
|
|
116
|
-
* "Resource": [
|
|
117
|
-
* "arn:aws:dynamodb:{region}:{account_id}:table/{table_name}",
|
|
118
|
-
* "arn:aws:dynamodb:{region}:{account_id}:table/{table_name}/index/GSI1"
|
|
119
|
-
* ]
|
|
120
|
-
* }
|
|
121
|
-
* ]
|
|
122
|
-
* }
|
|
123
|
-
* ```
|
|
124
|
-
*
|
|
125
|
-
* ## Advanced usage
|
|
126
|
-
*
|
|
127
|
-
* ### Default schema
|
|
128
|
-
*
|
|
129
|
-
* The table respects the single table design pattern. This has many advantages:
|
|
130
|
-
*
|
|
131
|
-
* - Only one table to manage, monitor and provision.
|
|
132
|
-
* - Querying relations is faster than with multi-table schemas (for eg. retrieving all sessions for a user).
|
|
133
|
-
* - Only one table needs to be replicated if you want to go multi-region.
|
|
134
|
-
*
|
|
135
|
-
* > This schema is adapted for use in DynamoDB and based upon our main [schema](https://authjs.dev/reference/core/adapters#models)
|
|
136
|
-
*
|
|
137
|
-
* 
|
|
138
|
-
*
|
|
139
|
-
* You can create this table with infrastructure as code using [`aws-cdk`](https://github.com/aws/aws-cdk) with the following table definition:
|
|
140
|
-
*
|
|
141
|
-
* ```javascript title=stack.ts
|
|
142
|
-
* new dynamodb.Table(this, `NextAuthTable`, {
|
|
143
|
-
* tableName: "next-auth",
|
|
144
|
-
* partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
|
|
145
|
-
* sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
|
|
146
|
-
* timeToLiveAttribute: "expires",
|
|
147
|
-
* }).addGlobalSecondaryIndex({
|
|
148
|
-
* indexName: "GSI1",
|
|
149
|
-
* partitionKey: { name: "GSI1PK", type: dynamodb.AttributeType.STRING },
|
|
150
|
-
* sortKey: { name: "GSI1SK", type: dynamodb.AttributeType.STRING },
|
|
151
|
-
* })
|
|
152
|
-
* ```
|
|
153
|
-
*
|
|
154
|
-
* Alternatively, you can use this cloudformation template:
|
|
155
|
-
*
|
|
156
|
-
* ```yaml title=cloudformation.yaml
|
|
157
|
-
* NextAuthTable:
|
|
158
|
-
* Type: "AWS::DynamoDB::Table"
|
|
159
|
-
* Properties:
|
|
160
|
-
* TableName: next-auth
|
|
161
|
-
* AttributeDefinitions:
|
|
162
|
-
* - AttributeName: pk
|
|
163
|
-
* AttributeType: S
|
|
164
|
-
* - AttributeName: sk
|
|
165
|
-
* AttributeType: S
|
|
166
|
-
* - AttributeName: GSI1PK
|
|
167
|
-
* AttributeType: S
|
|
168
|
-
* - AttributeName: GSI1SK
|
|
169
|
-
* AttributeType: S
|
|
170
|
-
* KeySchema:
|
|
171
|
-
* - AttributeName: pk
|
|
172
|
-
* KeyType: HASH
|
|
173
|
-
* - AttributeName: sk
|
|
174
|
-
* KeyType: RANGE
|
|
175
|
-
* GlobalSecondaryIndexes:
|
|
176
|
-
* - IndexName: GSI1
|
|
177
|
-
* Projection:
|
|
178
|
-
* ProjectionType: ALL
|
|
179
|
-
* KeySchema:
|
|
180
|
-
* - AttributeName: GSI1PK
|
|
181
|
-
* KeyType: HASH
|
|
182
|
-
* - AttributeName: GSI1SK
|
|
183
|
-
* KeyType: RANGE
|
|
184
|
-
* TimeToLiveSpecification:
|
|
185
|
-
* AttributeName: expires
|
|
186
|
-
* Enabled: true
|
|
187
|
-
* ```
|
|
188
|
-
*
|
|
189
|
-
* ### Using a custom schema
|
|
190
|
-
*
|
|
191
|
-
* You can configure your custom table schema by passing the `options` key to the adapter constructor:
|
|
192
|
-
*
|
|
193
|
-
* ```javascript
|
|
194
|
-
* const adapter = DynamoDBAdapter(client, {
|
|
195
|
-
* tableName: "custom-table-name",
|
|
196
|
-
* partitionKey: "custom-pk",
|
|
197
|
-
* sortKey: "custom-sk",
|
|
198
|
-
* indexName: "custom-index-name",
|
|
199
|
-
* indexPartitionKey: "custom-index-pk",
|
|
200
|
-
* indexSortKey: "custom-index-sk",
|
|
201
|
-
* })
|
|
202
|
-
* ```
|
|
203
|
-
**/
|
|
204
27
|
export declare function DynamoDBAdapter(client: DynamoDBDocument, options?: DynamoDBAdapterOptions): Adapter;
|
|
205
28
|
declare const format: {
|
|
206
|
-
/** Takes a plain old JavaScript object and turns it into a
|
|
29
|
+
/** Takes a plain old JavaScript object and turns it into a DynamoDB object */
|
|
207
30
|
to(object: Record<string, any>): Record<string, unknown>;
|
|
208
31
|
/** Takes a Dynamo object and returns a plain old JavaScript object */
|
|
209
32
|
from<T = Record<string, unknown>>(object?: Record<string, any>): T | null;
|
package/index.d.ts.map
CHANGED
|
@@ -1 +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
|
|
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,wBAAgB,eAAe,CAC7B,MAAM,EAAE,gBAAgB,EACxB,OAAO,CAAC,EAAE,sBAAsB,GAC/B,OAAO,CA+ST;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"}
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding:
|
|
3
|
-
* <p
|
|
2
|
+
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: "16px"}}>
|
|
3
|
+
* <p>Official <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">DynamoDB</a> adapter for Auth.js / NextAuth.js.</p>
|
|
4
4
|
* <a href="https://docs.aws.amazon.com/dynamodb/index.html">
|
|
5
5
|
* <img style={{display: "block"}} src="https://authjs.dev/img/adapters/dynamodb.png" width="48"/>
|
|
6
6
|
* </a>
|
|
@@ -14,183 +14,6 @@
|
|
|
14
14
|
*
|
|
15
15
|
* @module @auth/dynamodb-adapter
|
|
16
16
|
*/
|
|
17
|
-
/**
|
|
18
|
-
* ## Setup
|
|
19
|
-
*
|
|
20
|
-
* 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.
|
|
21
|
-
* You can find the full schema in the table structure section below.
|
|
22
|
-
*
|
|
23
|
-
* ### Configuring Auth.js
|
|
24
|
-
*
|
|
25
|
-
* 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.
|
|
26
|
-
* 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.
|
|
27
|
-
*
|
|
28
|
-
* ```javascript title="pages/api/auth/[...nextauth].js"
|
|
29
|
-
* import { DynamoDB, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb"
|
|
30
|
-
* import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb"
|
|
31
|
-
* import NextAuth from "next-auth";
|
|
32
|
-
* import Providers from "next-auth/providers";
|
|
33
|
-
* import { DynamoDBAdapter } from "@auth/dynamodb-adapter"
|
|
34
|
-
*
|
|
35
|
-
* const config: DynamoDBClientConfig = {
|
|
36
|
-
* credentials: {
|
|
37
|
-
* accessKeyId: process.env.NEXT_AUTH_AWS_ACCESS_KEY,
|
|
38
|
-
* secretAccessKey: process.env.NEXT_AUTH_AWS_SECRET_KEY,
|
|
39
|
-
* },
|
|
40
|
-
* region: process.env.NEXT_AUTH_AWS_REGION,
|
|
41
|
-
* };
|
|
42
|
-
*
|
|
43
|
-
* const client = DynamoDBDocument.from(new DynamoDB(config), {
|
|
44
|
-
* marshallOptions: {
|
|
45
|
-
* convertEmptyValues: true,
|
|
46
|
-
* removeUndefinedValues: true,
|
|
47
|
-
* convertClassInstanceToMap: true,
|
|
48
|
-
* },
|
|
49
|
-
* })
|
|
50
|
-
*
|
|
51
|
-
* export default NextAuth({
|
|
52
|
-
* // Configure one or more authentication providers
|
|
53
|
-
* providers: [
|
|
54
|
-
* Providers.GitHub({
|
|
55
|
-
* clientId: process.env.GITHUB_ID,
|
|
56
|
-
* clientSecret: process.env.GITHUB_SECRET,
|
|
57
|
-
* }),
|
|
58
|
-
* Providers.Email({
|
|
59
|
-
* server: process.env.EMAIL_SERVER,
|
|
60
|
-
* from: process.env.EMAIL_FROM,
|
|
61
|
-
* }),
|
|
62
|
-
* // ...add more providers here
|
|
63
|
-
* ],
|
|
64
|
-
* adapter: DynamoDBAdapter(
|
|
65
|
-
* client
|
|
66
|
-
* ),
|
|
67
|
-
* ...
|
|
68
|
-
* });
|
|
69
|
-
* ```
|
|
70
|
-
*
|
|
71
|
-
* (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).)
|
|
72
|
-
*
|
|
73
|
-
* ## AWS Credentials
|
|
74
|
-
*
|
|
75
|
-
* :::note
|
|
76
|
-
* Always follow the **principle of least privilege** when giving access to AWS
|
|
77
|
-
* services/resources -> identities should only be permitted to perform the
|
|
78
|
-
* smallest set of actions necessary to fulfill a specific task.
|
|
79
|
-
* :::
|
|
80
|
-
*
|
|
81
|
-
* 1. Open the [AWS console](https://console.aws.amazon.com/) and go to "IAM", then "Users".
|
|
82
|
-
* 2. Create a new user. The purpose of this user is to give programmatic access to DynamoDB.
|
|
83
|
-
* 3. Create an Access Key and then copy Key ID and Secret to your `.env`/`.env.local` file.
|
|
84
|
-
* 4. Select "Add Permission" and "Create Inline Policy".
|
|
85
|
-
* 5. Copy the JSON below into the JSON input and replace `region`, `account_id` and `table_name` with your values.
|
|
86
|
-
*
|
|
87
|
-
* ```json
|
|
88
|
-
* {
|
|
89
|
-
* "Version": "2012-10-17",
|
|
90
|
-
* "Statement": [
|
|
91
|
-
* {
|
|
92
|
-
* "Sid": "DynamoDBAccess",
|
|
93
|
-
* "Effect": "Allow",
|
|
94
|
-
* "Action": [
|
|
95
|
-
* "dynamodb:BatchGetItem",
|
|
96
|
-
* "dynamodb:BatchWriteItem",
|
|
97
|
-
* "dynamodb:Describe*",
|
|
98
|
-
* "dynamodb:List*",
|
|
99
|
-
* "dynamodb:PutItem",
|
|
100
|
-
* "dynamodb:DeleteItem",
|
|
101
|
-
* "dynamodb:GetItem",
|
|
102
|
-
* "dynamodb:Scan",
|
|
103
|
-
* "dynamodb:Query",
|
|
104
|
-
* "dynamodb:UpdateItem"
|
|
105
|
-
* ],
|
|
106
|
-
* "Resource": [
|
|
107
|
-
* "arn:aws:dynamodb:{region}:{account_id}:table/{table_name}",
|
|
108
|
-
* "arn:aws:dynamodb:{region}:{account_id}:table/{table_name}/index/GSI1"
|
|
109
|
-
* ]
|
|
110
|
-
* }
|
|
111
|
-
* ]
|
|
112
|
-
* }
|
|
113
|
-
* ```
|
|
114
|
-
*
|
|
115
|
-
* ## Advanced usage
|
|
116
|
-
*
|
|
117
|
-
* ### Default schema
|
|
118
|
-
*
|
|
119
|
-
* The table respects the single table design pattern. This has many advantages:
|
|
120
|
-
*
|
|
121
|
-
* - Only one table to manage, monitor and provision.
|
|
122
|
-
* - Querying relations is faster than with multi-table schemas (for eg. retrieving all sessions for a user).
|
|
123
|
-
* - Only one table needs to be replicated if you want to go multi-region.
|
|
124
|
-
*
|
|
125
|
-
* > This schema is adapted for use in DynamoDB and based upon our main [schema](https://authjs.dev/reference/core/adapters#models)
|
|
126
|
-
*
|
|
127
|
-
* 
|
|
128
|
-
*
|
|
129
|
-
* You can create this table with infrastructure as code using [`aws-cdk`](https://github.com/aws/aws-cdk) with the following table definition:
|
|
130
|
-
*
|
|
131
|
-
* ```javascript title=stack.ts
|
|
132
|
-
* new dynamodb.Table(this, `NextAuthTable`, {
|
|
133
|
-
* tableName: "next-auth",
|
|
134
|
-
* partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
|
|
135
|
-
* sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
|
|
136
|
-
* timeToLiveAttribute: "expires",
|
|
137
|
-
* }).addGlobalSecondaryIndex({
|
|
138
|
-
* indexName: "GSI1",
|
|
139
|
-
* partitionKey: { name: "GSI1PK", type: dynamodb.AttributeType.STRING },
|
|
140
|
-
* sortKey: { name: "GSI1SK", type: dynamodb.AttributeType.STRING },
|
|
141
|
-
* })
|
|
142
|
-
* ```
|
|
143
|
-
*
|
|
144
|
-
* Alternatively, you can use this cloudformation template:
|
|
145
|
-
*
|
|
146
|
-
* ```yaml title=cloudformation.yaml
|
|
147
|
-
* NextAuthTable:
|
|
148
|
-
* Type: "AWS::DynamoDB::Table"
|
|
149
|
-
* Properties:
|
|
150
|
-
* TableName: next-auth
|
|
151
|
-
* AttributeDefinitions:
|
|
152
|
-
* - AttributeName: pk
|
|
153
|
-
* AttributeType: S
|
|
154
|
-
* - AttributeName: sk
|
|
155
|
-
* AttributeType: S
|
|
156
|
-
* - AttributeName: GSI1PK
|
|
157
|
-
* AttributeType: S
|
|
158
|
-
* - AttributeName: GSI1SK
|
|
159
|
-
* AttributeType: S
|
|
160
|
-
* KeySchema:
|
|
161
|
-
* - AttributeName: pk
|
|
162
|
-
* KeyType: HASH
|
|
163
|
-
* - AttributeName: sk
|
|
164
|
-
* KeyType: RANGE
|
|
165
|
-
* GlobalSecondaryIndexes:
|
|
166
|
-
* - IndexName: GSI1
|
|
167
|
-
* Projection:
|
|
168
|
-
* ProjectionType: ALL
|
|
169
|
-
* KeySchema:
|
|
170
|
-
* - AttributeName: GSI1PK
|
|
171
|
-
* KeyType: HASH
|
|
172
|
-
* - AttributeName: GSI1SK
|
|
173
|
-
* KeyType: RANGE
|
|
174
|
-
* TimeToLiveSpecification:
|
|
175
|
-
* AttributeName: expires
|
|
176
|
-
* Enabled: true
|
|
177
|
-
* ```
|
|
178
|
-
*
|
|
179
|
-
* ### Using a custom schema
|
|
180
|
-
*
|
|
181
|
-
* You can configure your custom table schema by passing the `options` key to the adapter constructor:
|
|
182
|
-
*
|
|
183
|
-
* ```javascript
|
|
184
|
-
* const adapter = DynamoDBAdapter(client, {
|
|
185
|
-
* tableName: "custom-table-name",
|
|
186
|
-
* partitionKey: "custom-pk",
|
|
187
|
-
* sortKey: "custom-sk",
|
|
188
|
-
* indexName: "custom-index-name",
|
|
189
|
-
* indexPartitionKey: "custom-index-pk",
|
|
190
|
-
* indexSortKey: "custom-index-sk",
|
|
191
|
-
* })
|
|
192
|
-
* ```
|
|
193
|
-
**/
|
|
194
17
|
export function DynamoDBAdapter(client, options) {
|
|
195
18
|
const TableName = options?.tableName ?? "next-auth";
|
|
196
19
|
const pk = options?.partitionKey ?? "pk";
|
|
@@ -492,7 +315,7 @@ function isDate(value) {
|
|
|
492
315
|
return value && isoDateRE.test(value) && !isNaN(Date.parse(value));
|
|
493
316
|
}
|
|
494
317
|
const format = {
|
|
495
|
-
/** Takes a plain old JavaScript object and turns it into a
|
|
318
|
+
/** Takes a plain old JavaScript object and turns it into a DynamoDB object */
|
|
496
319
|
to(object) {
|
|
497
320
|
const newObject = {};
|
|
498
321
|
for (const key in object) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@auth/dynamodb-adapter",
|
|
3
3
|
"repository": "https://github.com/nextauthjs/next-auth",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "2.1.0",
|
|
5
5
|
"description": "AWS DynamoDB adapter for next-auth.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"next-auth",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@aws-sdk/lib-dynamodb": "^3.36.1"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@auth/core": "0.
|
|
47
|
+
"@auth/core": "0.31.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"test:default": "vitest -c ../utils/vitest.config.ts",
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding:
|
|
3
|
-
* <p
|
|
2
|
+
* <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: "16px"}}>
|
|
3
|
+
* <p>Official <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">DynamoDB</a> adapter for Auth.js / NextAuth.js.</p>
|
|
4
4
|
* <a href="https://docs.aws.amazon.com/dynamodb/index.html">
|
|
5
5
|
* <img style={{display: "block"}} src="https://authjs.dev/img/adapters/dynamodb.png" width="48"/>
|
|
6
6
|
* </a>
|
|
@@ -36,183 +36,6 @@ export interface DynamoDBAdapterOptions {
|
|
|
36
36
|
indexSortKey?: string
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
/**
|
|
40
|
-
* ## Setup
|
|
41
|
-
*
|
|
42
|
-
* 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.
|
|
43
|
-
* You can find the full schema in the table structure section below.
|
|
44
|
-
*
|
|
45
|
-
* ### Configuring Auth.js
|
|
46
|
-
*
|
|
47
|
-
* 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.
|
|
48
|
-
* 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.
|
|
49
|
-
*
|
|
50
|
-
* ```javascript title="pages/api/auth/[...nextauth].js"
|
|
51
|
-
* import { DynamoDB, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb"
|
|
52
|
-
* import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb"
|
|
53
|
-
* import NextAuth from "next-auth";
|
|
54
|
-
* import Providers from "next-auth/providers";
|
|
55
|
-
* import { DynamoDBAdapter } from "@auth/dynamodb-adapter"
|
|
56
|
-
*
|
|
57
|
-
* const config: DynamoDBClientConfig = {
|
|
58
|
-
* credentials: {
|
|
59
|
-
* accessKeyId: process.env.NEXT_AUTH_AWS_ACCESS_KEY,
|
|
60
|
-
* secretAccessKey: process.env.NEXT_AUTH_AWS_SECRET_KEY,
|
|
61
|
-
* },
|
|
62
|
-
* region: process.env.NEXT_AUTH_AWS_REGION,
|
|
63
|
-
* };
|
|
64
|
-
*
|
|
65
|
-
* const client = DynamoDBDocument.from(new DynamoDB(config), {
|
|
66
|
-
* marshallOptions: {
|
|
67
|
-
* convertEmptyValues: true,
|
|
68
|
-
* removeUndefinedValues: true,
|
|
69
|
-
* convertClassInstanceToMap: true,
|
|
70
|
-
* },
|
|
71
|
-
* })
|
|
72
|
-
*
|
|
73
|
-
* export default NextAuth({
|
|
74
|
-
* // Configure one or more authentication providers
|
|
75
|
-
* providers: [
|
|
76
|
-
* Providers.GitHub({
|
|
77
|
-
* clientId: process.env.GITHUB_ID,
|
|
78
|
-
* clientSecret: process.env.GITHUB_SECRET,
|
|
79
|
-
* }),
|
|
80
|
-
* Providers.Email({
|
|
81
|
-
* server: process.env.EMAIL_SERVER,
|
|
82
|
-
* from: process.env.EMAIL_FROM,
|
|
83
|
-
* }),
|
|
84
|
-
* // ...add more providers here
|
|
85
|
-
* ],
|
|
86
|
-
* adapter: DynamoDBAdapter(
|
|
87
|
-
* client
|
|
88
|
-
* ),
|
|
89
|
-
* ...
|
|
90
|
-
* });
|
|
91
|
-
* ```
|
|
92
|
-
*
|
|
93
|
-
* (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).)
|
|
94
|
-
*
|
|
95
|
-
* ## AWS Credentials
|
|
96
|
-
*
|
|
97
|
-
* :::note
|
|
98
|
-
* Always follow the **principle of least privilege** when giving access to AWS
|
|
99
|
-
* services/resources -> identities should only be permitted to perform the
|
|
100
|
-
* smallest set of actions necessary to fulfill a specific task.
|
|
101
|
-
* :::
|
|
102
|
-
*
|
|
103
|
-
* 1. Open the [AWS console](https://console.aws.amazon.com/) and go to "IAM", then "Users".
|
|
104
|
-
* 2. Create a new user. The purpose of this user is to give programmatic access to DynamoDB.
|
|
105
|
-
* 3. Create an Access Key and then copy Key ID and Secret to your `.env`/`.env.local` file.
|
|
106
|
-
* 4. Select "Add Permission" and "Create Inline Policy".
|
|
107
|
-
* 5. Copy the JSON below into the JSON input and replace `region`, `account_id` and `table_name` with your values.
|
|
108
|
-
*
|
|
109
|
-
* ```json
|
|
110
|
-
* {
|
|
111
|
-
* "Version": "2012-10-17",
|
|
112
|
-
* "Statement": [
|
|
113
|
-
* {
|
|
114
|
-
* "Sid": "DynamoDBAccess",
|
|
115
|
-
* "Effect": "Allow",
|
|
116
|
-
* "Action": [
|
|
117
|
-
* "dynamodb:BatchGetItem",
|
|
118
|
-
* "dynamodb:BatchWriteItem",
|
|
119
|
-
* "dynamodb:Describe*",
|
|
120
|
-
* "dynamodb:List*",
|
|
121
|
-
* "dynamodb:PutItem",
|
|
122
|
-
* "dynamodb:DeleteItem",
|
|
123
|
-
* "dynamodb:GetItem",
|
|
124
|
-
* "dynamodb:Scan",
|
|
125
|
-
* "dynamodb:Query",
|
|
126
|
-
* "dynamodb:UpdateItem"
|
|
127
|
-
* ],
|
|
128
|
-
* "Resource": [
|
|
129
|
-
* "arn:aws:dynamodb:{region}:{account_id}:table/{table_name}",
|
|
130
|
-
* "arn:aws:dynamodb:{region}:{account_id}:table/{table_name}/index/GSI1"
|
|
131
|
-
* ]
|
|
132
|
-
* }
|
|
133
|
-
* ]
|
|
134
|
-
* }
|
|
135
|
-
* ```
|
|
136
|
-
*
|
|
137
|
-
* ## Advanced usage
|
|
138
|
-
*
|
|
139
|
-
* ### Default schema
|
|
140
|
-
*
|
|
141
|
-
* The table respects the single table design pattern. This has many advantages:
|
|
142
|
-
*
|
|
143
|
-
* - Only one table to manage, monitor and provision.
|
|
144
|
-
* - Querying relations is faster than with multi-table schemas (for eg. retrieving all sessions for a user).
|
|
145
|
-
* - Only one table needs to be replicated if you want to go multi-region.
|
|
146
|
-
*
|
|
147
|
-
* > This schema is adapted for use in DynamoDB and based upon our main [schema](https://authjs.dev/reference/core/adapters#models)
|
|
148
|
-
*
|
|
149
|
-
* 
|
|
150
|
-
*
|
|
151
|
-
* You can create this table with infrastructure as code using [`aws-cdk`](https://github.com/aws/aws-cdk) with the following table definition:
|
|
152
|
-
*
|
|
153
|
-
* ```javascript title=stack.ts
|
|
154
|
-
* new dynamodb.Table(this, `NextAuthTable`, {
|
|
155
|
-
* tableName: "next-auth",
|
|
156
|
-
* partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
|
|
157
|
-
* sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
|
|
158
|
-
* timeToLiveAttribute: "expires",
|
|
159
|
-
* }).addGlobalSecondaryIndex({
|
|
160
|
-
* indexName: "GSI1",
|
|
161
|
-
* partitionKey: { name: "GSI1PK", type: dynamodb.AttributeType.STRING },
|
|
162
|
-
* sortKey: { name: "GSI1SK", type: dynamodb.AttributeType.STRING },
|
|
163
|
-
* })
|
|
164
|
-
* ```
|
|
165
|
-
*
|
|
166
|
-
* Alternatively, you can use this cloudformation template:
|
|
167
|
-
*
|
|
168
|
-
* ```yaml title=cloudformation.yaml
|
|
169
|
-
* NextAuthTable:
|
|
170
|
-
* Type: "AWS::DynamoDB::Table"
|
|
171
|
-
* Properties:
|
|
172
|
-
* TableName: next-auth
|
|
173
|
-
* AttributeDefinitions:
|
|
174
|
-
* - AttributeName: pk
|
|
175
|
-
* AttributeType: S
|
|
176
|
-
* - AttributeName: sk
|
|
177
|
-
* AttributeType: S
|
|
178
|
-
* - AttributeName: GSI1PK
|
|
179
|
-
* AttributeType: S
|
|
180
|
-
* - AttributeName: GSI1SK
|
|
181
|
-
* AttributeType: S
|
|
182
|
-
* KeySchema:
|
|
183
|
-
* - AttributeName: pk
|
|
184
|
-
* KeyType: HASH
|
|
185
|
-
* - AttributeName: sk
|
|
186
|
-
* KeyType: RANGE
|
|
187
|
-
* GlobalSecondaryIndexes:
|
|
188
|
-
* - IndexName: GSI1
|
|
189
|
-
* Projection:
|
|
190
|
-
* ProjectionType: ALL
|
|
191
|
-
* KeySchema:
|
|
192
|
-
* - AttributeName: GSI1PK
|
|
193
|
-
* KeyType: HASH
|
|
194
|
-
* - AttributeName: GSI1SK
|
|
195
|
-
* KeyType: RANGE
|
|
196
|
-
* TimeToLiveSpecification:
|
|
197
|
-
* AttributeName: expires
|
|
198
|
-
* Enabled: true
|
|
199
|
-
* ```
|
|
200
|
-
*
|
|
201
|
-
* ### Using a custom schema
|
|
202
|
-
*
|
|
203
|
-
* You can configure your custom table schema by passing the `options` key to the adapter constructor:
|
|
204
|
-
*
|
|
205
|
-
* ```javascript
|
|
206
|
-
* const adapter = DynamoDBAdapter(client, {
|
|
207
|
-
* tableName: "custom-table-name",
|
|
208
|
-
* partitionKey: "custom-pk",
|
|
209
|
-
* sortKey: "custom-sk",
|
|
210
|
-
* indexName: "custom-index-name",
|
|
211
|
-
* indexPartitionKey: "custom-index-pk",
|
|
212
|
-
* indexSortKey: "custom-index-sk",
|
|
213
|
-
* })
|
|
214
|
-
* ```
|
|
215
|
-
**/
|
|
216
39
|
export function DynamoDBAdapter(
|
|
217
40
|
client: DynamoDBDocument,
|
|
218
41
|
options?: DynamoDBAdapterOptions
|
|
@@ -529,7 +352,7 @@ function isDate(value: any) {
|
|
|
529
352
|
}
|
|
530
353
|
|
|
531
354
|
const format = {
|
|
532
|
-
/** Takes a plain old JavaScript object and turns it into a
|
|
355
|
+
/** Takes a plain old JavaScript object and turns it into a DynamoDB object */
|
|
533
356
|
to(object: Record<string, any>) {
|
|
534
357
|
const newObject: Record<string, unknown> = {}
|
|
535
358
|
for (const key in object) {
|