@auth/dgraph-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 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/logo-sm.png" />
4
+ <img height="64px" src="https://authjs.dev/img/logo-sm.png" />
5
5
  </a>
6
6
  <a href="https://dgraph.io" target="_blank">
7
7
  <img height="64px" src="https://authjs.dev/img/adapters/dgraph.svg"/>
@@ -18,7 +18,7 @@
18
18
  <img src="https://img.shields.io/npm/dm/@auth/dgraph-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="Github Stars" />
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
@@ -19,237 +19,5 @@ export interface DgraphAdapterOptions {
19
19
  };
20
20
  }
21
21
  export { format };
22
- /**
23
- * ## Setup
24
- *
25
- * Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object:
26
- *
27
- * ```ts title="pages/api/auth/[...nextauth].js"
28
- * import NextAuth from "next-auth"
29
- * import { DgraphAdapter } from "@auth/dgraph-adapter"
30
- *
31
- * export default NextAuth({
32
- * providers: [],
33
- * adapter: DgraphAdapter({
34
- * endpoint: process.env.DGRAPH_GRAPHQL_ENDPOINT,
35
- * authToken: process.env.DGRAPH_GRAPHQL_KEY,
36
- * // you can omit the following properties if you are running an unsecure schema
37
- * authHeader: process.env.AUTH_HEADER, // default: "Authorization",
38
- * jwtSecret: process.env.SECRET,
39
- * }),
40
- * })
41
- * ```
42
- *
43
- * ### Unsecure schema
44
- *
45
- * The quickest way to use Dgraph is by applying the unsecure schema to your [local](https://dgraph.io/docs/graphql/admin/#modifying-a-schema) Dgraph instance or if using Dgraph [cloud](https://dgraph.io/docs/cloud/cloud-quick-start/#the-schema) you can paste the schema in the codebox to update.
46
- *
47
- * :::warning
48
- * This approach is not secure or for production use, and does not require a `jwtSecret`.
49
- * :::
50
- *
51
- * > This schema is adapted for use in Dgraph and based upon our main [schema](https://authjs.dev/reference/core/adapters)
52
- *
53
- * #### Example
54
- *
55
- *```graphql
56
- * type Account {
57
- * id: ID
58
- * type: String
59
- * provider: String @search(by: [hash])
60
- * providerAccountId: String @search(by: [hash])
61
- * refreshToken: String
62
- * expires_at: Int64
63
- * accessToken: String
64
- * token_type: String
65
- * refresh_token: String
66
- * access_token: String
67
- * scope: String
68
- * id_token: String
69
- * session_state: String
70
- * user: User @hasInverse(field: "accounts")
71
- * }
72
- * type Session {
73
- * id: ID
74
- * expires: DateTime
75
- * sessionToken: String @search(by: [hash])
76
- * user: User @hasInverse(field: "sessions")
77
- * }
78
- * type User {
79
- * id: ID
80
- * name: String
81
- * email: String @search(by: [hash])
82
- * emailVerified: DateTime
83
- * image: String
84
- * accounts: [Account] @hasInverse(field: "user")
85
- * sessions: [Session] @hasInverse(field: "user")
86
- * }
87
- *
88
- * type VerificationToken {
89
- * id: ID
90
- * identifier: String @search(by: [hash])
91
- * token: String @search(by: [hash])
92
- * expires: DateTime
93
- * }
94
- *```
95
- *
96
- * ### Secure schema
97
- *
98
- * For production deployments you will want to restrict the access to the types used
99
- * by next-auth. The main form of access control used in Dgraph is via `@auth` directive alongside types in the schema.
100
- * #### Example
101
- *
102
- * ```graphql
103
- * type Account
104
- * @auth(
105
- * delete: { rule: "{$nextAuth: { eq: true } }" }
106
- * add: { rule: "{$nextAuth: { eq: true } }" }
107
- * query: { rule: "{$nextAuth: { eq: true } }" }
108
- * update: { rule: "{$nextAuth: { eq: true } }" }
109
- * ) {
110
- * id: ID
111
- * type: String
112
- * provider: String @search(by: [hash])
113
- * providerAccountId: String @search(by: [hash])
114
- * refreshToken: String
115
- * expires_at: Int64
116
- * accessToken: String
117
- * token_type: String
118
- * refresh_token: String
119
- * access_token: String
120
- * scope: String
121
- * id_token: String
122
- * session_state: String
123
- * user: User @hasInverse(field: "accounts")
124
- * }
125
- * type Session
126
- * @auth(
127
- * delete: { rule: "{$nextAuth: { eq: true } }" }
128
- * add: { rule: "{$nextAuth: { eq: true } }" }
129
- * query: { rule: "{$nextAuth: { eq: true } }" }
130
- * update: { rule: "{$nextAuth: { eq: true } }" }
131
- * ) {
132
- * id: ID
133
- * expires: DateTime
134
- * sessionToken: String @search(by: [hash])
135
- * user: User @hasInverse(field: "sessions")
136
- * }
137
- * type User
138
- * @auth(
139
- * query: {
140
- * or: [
141
- * {
142
- * rule: """
143
- * query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
144
- * """
145
- * }
146
- * { rule: "{$nextAuth: { eq: true } }" }
147
- * ]
148
- * }
149
- * delete: { rule: "{$nextAuth: { eq: true } }" }
150
- * add: { rule: "{$nextAuth: { eq: true } }" }
151
- * update: {
152
- * or: [
153
- * {
154
- * rule: """
155
- * query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
156
- * """
157
- * }
158
- * { rule: "{$nextAuth: { eq: true } }" }
159
- * ]
160
- * }
161
- * ) {
162
- * id: ID
163
- * name: String
164
- * email: String @search(by: [hash])
165
- * emailVerified: DateTime
166
- * image: String
167
- * accounts: [Account] @hasInverse(field: "user")
168
- * sessions: [Session] @hasInverse(field: "user")
169
- * }
170
- *
171
- * type VerificationToken
172
- * @auth(
173
- * delete: { rule: "{$nextAuth: { eq: true } }" }
174
- * add: { rule: "{$nextAuth: { eq: true } }" }
175
- * query: { rule: "{$nextAuth: { eq: true } }" }
176
- * update: { rule: "{$nextAuth: { eq: true } }" }
177
- * ) {
178
- * id: ID
179
- * identifier: String @search(by: [hash])
180
- * token: String @search(by: [hash])
181
- * expires: DateTime
182
- * }
183
- *
184
- * # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"<YOUR CUSTOM NAMESPACE HERE>","Algo":"HS256"}
185
- * ```
186
- *
187
- * ### Dgraph.Authorization
188
- *
189
- * In order to secure your graphql backend define the `Dgraph.Authorization` object at the
190
- * bottom of your schema and provide `authHeader` and `jwtSecret` values to the DgraphClient.
191
- *
192
- * ```js
193
- * # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"YOUR CUSTOM NAMESPACE HERE","Algo":"HS256"}
194
- * ```
195
- *
196
- * ### VerificationKey and jwtSecret
197
- *
198
- * This is the key used to sign the JWT. Ex. `process.env.SECRET` or `process.env.APP_SECRET`.
199
- *
200
- * ### Header and authHeader
201
- *
202
- * The `Header` tells Dgraph where to lookup a JWT within the headers of the incoming requests made to the dgraph server.
203
- * You have to configure it at the bottom of your schema file. This header is the same as the `authHeader` property you
204
- * provide when you instantiate the `DgraphClient`.
205
- *
206
- * ### The nextAuth secret
207
- *
208
- * The `$nextAuth` secret is securely generated using the `jwtSecret` and injected by the DgraphAdapter in order to allow interacting with the JWT DgraphClient for anonymous user requests made within the system `ie. login, register`. This allows
209
- * secure interactions to be made with all the auth types required by next-auth. You have to specify it for each auth rule of
210
- * each type defined in your secure schema.
211
- *
212
- * ```js
213
- * type VerificationRequest
214
- * @auth(
215
- * delete: { rule: "{$nextAuth: { eq: true } }" },
216
- * add: { rule: "{$nextAuth: { eq: true } }" },
217
- * query: { rule: "{$nextAuth: { eq: true } }" },
218
- * update: { rule: "{$nextAuth: { eq: true } }" }
219
- * ) {
220
- * ...
221
- * }
222
- * ```
223
- *
224
- * ### JWT session and `@auth` directive
225
- *
226
- * Dgraph only works with HS256 or RS256 algorithms. If you want to use session jwt to securely interact with your dgraph
227
- * database you must customize next-auth `encode` and `decode` functions, as the default algorithm is HS512. You can
228
- * further customize the jwt with roles if you want to implement [`RBAC logic`](https://dgraph.io/docs/graphql/authorization/directive/#role-based-access-control).
229
- *
230
- * ```js
231
- * import * as jwt from "jsonwebtoken"
232
- * export default NextAuth({
233
- * session: {
234
- * strategy: "jwt",
235
- * },
236
- * jwt: {
237
- * secret: process.env.SECRET,
238
- * encode: async ({ secret, token }) => {
239
- * return jwt.sign({ ...token, userId: token.id }, secret, {
240
- * algorithm: "HS256",
241
- * expiresIn: 30 * 24 * 60 * 60, // 30 days
242
- * })
243
- * },
244
- * decode: async ({ secret, token }) => {
245
- * return jwt.verify(token, secret, { algorithms: ["HS256"] })
246
- * },
247
- * },
248
- * })
249
- * ```
250
- *
251
- * Once your `Dgraph.Authorization` is defined in your schema and the JWT settings are set, this will allow you to define
252
- * [`@auth rules`](https://dgraph.io/docs/graphql/authorization/authorization-overview/) for every part of your schema.
253
- **/
254
22
  export declare function DgraphAdapter(client: DgraphClientParams, options?: DgraphAdapterOptions): Adapter;
255
23
  //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAGtD,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEzE,2DAA2D;AAC3D,MAAM,WAAW,oBAAoB;IACnC;;;;;;OAMG;IACH,SAAS,CAAC,EAAE;QACV,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,iBAAiB,CAAC,EAAE,MAAM,CAAA;KAC3B,CAAA;CACF;AAED,OAAO,EAAE,MAAM,EAAE,CAAA;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAuOI;AACJ,wBAAgB,aAAa,CAC3B,MAAM,EAAE,kBAAkB,EAC1B,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAmST"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAGtD,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA;AAEzE,2DAA2D;AAC3D,MAAM,WAAW,oBAAoB;IACnC;;;;;;OAMG;IACH,SAAS,CAAC,EAAE;QACV,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,iBAAiB,CAAC,EAAE,MAAM,CAAA;KAC3B,CAAA;CACF;AAED,OAAO,EAAE,MAAM,EAAE,CAAA;AAEjB,wBAAgB,aAAa,CAC3B,MAAM,EAAE,kBAAkB,EAC1B,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAmST"}
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
2
+ * <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: "16px"}}>
3
3
  * <p style={{fontWeight: "normal"}}>Official <a href="https://dgraph.io/docs">Dgraph</a> adapter for Auth.js / NextAuth.js.</p>
4
4
  * <a href="https://dgraph.io/">
5
5
  * <img style={{display: "block"}} src="https://authjs.dev/img/adapters/dgraph.svg" width="100"/>
@@ -18,238 +18,6 @@ import { client as dgraphClient } from "./lib/client";
18
18
  import { format } from "./lib/utils";
19
19
  import * as defaultFragments from "./lib/graphql/fragments";
20
20
  export { format };
21
- /**
22
- * ## Setup
23
- *
24
- * Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object:
25
- *
26
- * ```ts title="pages/api/auth/[...nextauth].js"
27
- * import NextAuth from "next-auth"
28
- * import { DgraphAdapter } from "@auth/dgraph-adapter"
29
- *
30
- * export default NextAuth({
31
- * providers: [],
32
- * adapter: DgraphAdapter({
33
- * endpoint: process.env.DGRAPH_GRAPHQL_ENDPOINT,
34
- * authToken: process.env.DGRAPH_GRAPHQL_KEY,
35
- * // you can omit the following properties if you are running an unsecure schema
36
- * authHeader: process.env.AUTH_HEADER, // default: "Authorization",
37
- * jwtSecret: process.env.SECRET,
38
- * }),
39
- * })
40
- * ```
41
- *
42
- * ### Unsecure schema
43
- *
44
- * The quickest way to use Dgraph is by applying the unsecure schema to your [local](https://dgraph.io/docs/graphql/admin/#modifying-a-schema) Dgraph instance or if using Dgraph [cloud](https://dgraph.io/docs/cloud/cloud-quick-start/#the-schema) you can paste the schema in the codebox to update.
45
- *
46
- * :::warning
47
- * This approach is not secure or for production use, and does not require a `jwtSecret`.
48
- * :::
49
- *
50
- * > This schema is adapted for use in Dgraph and based upon our main [schema](https://authjs.dev/reference/core/adapters)
51
- *
52
- * #### Example
53
- *
54
- *```graphql
55
- * type Account {
56
- * id: ID
57
- * type: String
58
- * provider: String @search(by: [hash])
59
- * providerAccountId: String @search(by: [hash])
60
- * refreshToken: String
61
- * expires_at: Int64
62
- * accessToken: String
63
- * token_type: String
64
- * refresh_token: String
65
- * access_token: String
66
- * scope: String
67
- * id_token: String
68
- * session_state: String
69
- * user: User @hasInverse(field: "accounts")
70
- * }
71
- * type Session {
72
- * id: ID
73
- * expires: DateTime
74
- * sessionToken: String @search(by: [hash])
75
- * user: User @hasInverse(field: "sessions")
76
- * }
77
- * type User {
78
- * id: ID
79
- * name: String
80
- * email: String @search(by: [hash])
81
- * emailVerified: DateTime
82
- * image: String
83
- * accounts: [Account] @hasInverse(field: "user")
84
- * sessions: [Session] @hasInverse(field: "user")
85
- * }
86
- *
87
- * type VerificationToken {
88
- * id: ID
89
- * identifier: String @search(by: [hash])
90
- * token: String @search(by: [hash])
91
- * expires: DateTime
92
- * }
93
- *```
94
- *
95
- * ### Secure schema
96
- *
97
- * For production deployments you will want to restrict the access to the types used
98
- * by next-auth. The main form of access control used in Dgraph is via `@auth` directive alongside types in the schema.
99
- * #### Example
100
- *
101
- * ```graphql
102
- * type Account
103
- * @auth(
104
- * delete: { rule: "{$nextAuth: { eq: true } }" }
105
- * add: { rule: "{$nextAuth: { eq: true } }" }
106
- * query: { rule: "{$nextAuth: { eq: true } }" }
107
- * update: { rule: "{$nextAuth: { eq: true } }" }
108
- * ) {
109
- * id: ID
110
- * type: String
111
- * provider: String @search(by: [hash])
112
- * providerAccountId: String @search(by: [hash])
113
- * refreshToken: String
114
- * expires_at: Int64
115
- * accessToken: String
116
- * token_type: String
117
- * refresh_token: String
118
- * access_token: String
119
- * scope: String
120
- * id_token: String
121
- * session_state: String
122
- * user: User @hasInverse(field: "accounts")
123
- * }
124
- * type Session
125
- * @auth(
126
- * delete: { rule: "{$nextAuth: { eq: true } }" }
127
- * add: { rule: "{$nextAuth: { eq: true } }" }
128
- * query: { rule: "{$nextAuth: { eq: true } }" }
129
- * update: { rule: "{$nextAuth: { eq: true } }" }
130
- * ) {
131
- * id: ID
132
- * expires: DateTime
133
- * sessionToken: String @search(by: [hash])
134
- * user: User @hasInverse(field: "sessions")
135
- * }
136
- * type User
137
- * @auth(
138
- * query: {
139
- * or: [
140
- * {
141
- * rule: """
142
- * query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
143
- * """
144
- * }
145
- * { rule: "{$nextAuth: { eq: true } }" }
146
- * ]
147
- * }
148
- * delete: { rule: "{$nextAuth: { eq: true } }" }
149
- * add: { rule: "{$nextAuth: { eq: true } }" }
150
- * update: {
151
- * or: [
152
- * {
153
- * rule: """
154
- * query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
155
- * """
156
- * }
157
- * { rule: "{$nextAuth: { eq: true } }" }
158
- * ]
159
- * }
160
- * ) {
161
- * id: ID
162
- * name: String
163
- * email: String @search(by: [hash])
164
- * emailVerified: DateTime
165
- * image: String
166
- * accounts: [Account] @hasInverse(field: "user")
167
- * sessions: [Session] @hasInverse(field: "user")
168
- * }
169
- *
170
- * type VerificationToken
171
- * @auth(
172
- * delete: { rule: "{$nextAuth: { eq: true } }" }
173
- * add: { rule: "{$nextAuth: { eq: true } }" }
174
- * query: { rule: "{$nextAuth: { eq: true } }" }
175
- * update: { rule: "{$nextAuth: { eq: true } }" }
176
- * ) {
177
- * id: ID
178
- * identifier: String @search(by: [hash])
179
- * token: String @search(by: [hash])
180
- * expires: DateTime
181
- * }
182
- *
183
- * # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"<YOUR CUSTOM NAMESPACE HERE>","Algo":"HS256"}
184
- * ```
185
- *
186
- * ### Dgraph.Authorization
187
- *
188
- * In order to secure your graphql backend define the `Dgraph.Authorization` object at the
189
- * bottom of your schema and provide `authHeader` and `jwtSecret` values to the DgraphClient.
190
- *
191
- * ```js
192
- * # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"YOUR CUSTOM NAMESPACE HERE","Algo":"HS256"}
193
- * ```
194
- *
195
- * ### VerificationKey and jwtSecret
196
- *
197
- * This is the key used to sign the JWT. Ex. `process.env.SECRET` or `process.env.APP_SECRET`.
198
- *
199
- * ### Header and authHeader
200
- *
201
- * The `Header` tells Dgraph where to lookup a JWT within the headers of the incoming requests made to the dgraph server.
202
- * You have to configure it at the bottom of your schema file. This header is the same as the `authHeader` property you
203
- * provide when you instantiate the `DgraphClient`.
204
- *
205
- * ### The nextAuth secret
206
- *
207
- * The `$nextAuth` secret is securely generated using the `jwtSecret` and injected by the DgraphAdapter in order to allow interacting with the JWT DgraphClient for anonymous user requests made within the system `ie. login, register`. This allows
208
- * secure interactions to be made with all the auth types required by next-auth. You have to specify it for each auth rule of
209
- * each type defined in your secure schema.
210
- *
211
- * ```js
212
- * type VerificationRequest
213
- * @auth(
214
- * delete: { rule: "{$nextAuth: { eq: true } }" },
215
- * add: { rule: "{$nextAuth: { eq: true } }" },
216
- * query: { rule: "{$nextAuth: { eq: true } }" },
217
- * update: { rule: "{$nextAuth: { eq: true } }" }
218
- * ) {
219
- * ...
220
- * }
221
- * ```
222
- *
223
- * ### JWT session and `@auth` directive
224
- *
225
- * Dgraph only works with HS256 or RS256 algorithms. If you want to use session jwt to securely interact with your dgraph
226
- * database you must customize next-auth `encode` and `decode` functions, as the default algorithm is HS512. You can
227
- * further customize the jwt with roles if you want to implement [`RBAC logic`](https://dgraph.io/docs/graphql/authorization/directive/#role-based-access-control).
228
- *
229
- * ```js
230
- * import * as jwt from "jsonwebtoken"
231
- * export default NextAuth({
232
- * session: {
233
- * strategy: "jwt",
234
- * },
235
- * jwt: {
236
- * secret: process.env.SECRET,
237
- * encode: async ({ secret, token }) => {
238
- * return jwt.sign({ ...token, userId: token.id }, secret, {
239
- * algorithm: "HS256",
240
- * expiresIn: 30 * 24 * 60 * 60, // 30 days
241
- * })
242
- * },
243
- * decode: async ({ secret, token }) => {
244
- * return jwt.verify(token, secret, { algorithms: ["HS256"] })
245
- * },
246
- * },
247
- * })
248
- * ```
249
- *
250
- * Once your `Dgraph.Authorization` is defined in your schema and the JWT settings are set, this will allow you to define
251
- * [`@auth rules`](https://dgraph.io/docs/graphql/authorization/authorization-overview/) for every part of your schema.
252
- **/
253
21
  export function DgraphAdapter(client, options) {
254
22
  const c = dgraphClient(client);
255
23
  const fragments = { ...defaultFragments, ...options?.fragments };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auth/dgraph-adapter",
3
- "version": "1.6.0",
3
+ "version": "2.1.0",
4
4
  "description": "Dgraph adapter for Auth.js",
5
5
  "homepage": "https://authjs.dev",
6
6
  "repository": "https://github.com/nextauthjs/next-auth",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "jsonwebtoken": "^9.0.0",
44
- "@auth/core": "0.29.0"
44
+ "@auth/core": "0.31.0"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "tsc",
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: 16}}>
2
+ * <div style={{display: "flex", justifyContent: "space-between", alignItems: "center", padding: "16px"}}>
3
3
  * <p style={{fontWeight: "normal"}}>Official <a href="https://dgraph.io/docs">Dgraph</a> adapter for Auth.js / NextAuth.js.</p>
4
4
  * <a href="https://dgraph.io/">
5
5
  * <img style={{display: "block"}} src="https://authjs.dev/img/adapters/dgraph.svg" width="100"/>
@@ -41,238 +41,6 @@ export interface DgraphAdapterOptions {
41
41
 
42
42
  export { format }
43
43
 
44
- /**
45
- * ## Setup
46
- *
47
- * Add this adapter to your `pages/api/[...nextauth].js` next-auth configuration object:
48
- *
49
- * ```ts title="pages/api/auth/[...nextauth].js"
50
- * import NextAuth from "next-auth"
51
- * import { DgraphAdapter } from "@auth/dgraph-adapter"
52
- *
53
- * export default NextAuth({
54
- * providers: [],
55
- * adapter: DgraphAdapter({
56
- * endpoint: process.env.DGRAPH_GRAPHQL_ENDPOINT,
57
- * authToken: process.env.DGRAPH_GRAPHQL_KEY,
58
- * // you can omit the following properties if you are running an unsecure schema
59
- * authHeader: process.env.AUTH_HEADER, // default: "Authorization",
60
- * jwtSecret: process.env.SECRET,
61
- * }),
62
- * })
63
- * ```
64
- *
65
- * ### Unsecure schema
66
- *
67
- * The quickest way to use Dgraph is by applying the unsecure schema to your [local](https://dgraph.io/docs/graphql/admin/#modifying-a-schema) Dgraph instance or if using Dgraph [cloud](https://dgraph.io/docs/cloud/cloud-quick-start/#the-schema) you can paste the schema in the codebox to update.
68
- *
69
- * :::warning
70
- * This approach is not secure or for production use, and does not require a `jwtSecret`.
71
- * :::
72
- *
73
- * > This schema is adapted for use in Dgraph and based upon our main [schema](https://authjs.dev/reference/core/adapters)
74
- *
75
- * #### Example
76
- *
77
- *```graphql
78
- * type Account {
79
- * id: ID
80
- * type: String
81
- * provider: String @search(by: [hash])
82
- * providerAccountId: String @search(by: [hash])
83
- * refreshToken: String
84
- * expires_at: Int64
85
- * accessToken: String
86
- * token_type: String
87
- * refresh_token: String
88
- * access_token: String
89
- * scope: String
90
- * id_token: String
91
- * session_state: String
92
- * user: User @hasInverse(field: "accounts")
93
- * }
94
- * type Session {
95
- * id: ID
96
- * expires: DateTime
97
- * sessionToken: String @search(by: [hash])
98
- * user: User @hasInverse(field: "sessions")
99
- * }
100
- * type User {
101
- * id: ID
102
- * name: String
103
- * email: String @search(by: [hash])
104
- * emailVerified: DateTime
105
- * image: String
106
- * accounts: [Account] @hasInverse(field: "user")
107
- * sessions: [Session] @hasInverse(field: "user")
108
- * }
109
- *
110
- * type VerificationToken {
111
- * id: ID
112
- * identifier: String @search(by: [hash])
113
- * token: String @search(by: [hash])
114
- * expires: DateTime
115
- * }
116
- *```
117
- *
118
- * ### Secure schema
119
- *
120
- * For production deployments you will want to restrict the access to the types used
121
- * by next-auth. The main form of access control used in Dgraph is via `@auth` directive alongside types in the schema.
122
- * #### Example
123
- *
124
- * ```graphql
125
- * type Account
126
- * @auth(
127
- * delete: { rule: "{$nextAuth: { eq: true } }" }
128
- * add: { rule: "{$nextAuth: { eq: true } }" }
129
- * query: { rule: "{$nextAuth: { eq: true } }" }
130
- * update: { rule: "{$nextAuth: { eq: true } }" }
131
- * ) {
132
- * id: ID
133
- * type: String
134
- * provider: String @search(by: [hash])
135
- * providerAccountId: String @search(by: [hash])
136
- * refreshToken: String
137
- * expires_at: Int64
138
- * accessToken: String
139
- * token_type: String
140
- * refresh_token: String
141
- * access_token: String
142
- * scope: String
143
- * id_token: String
144
- * session_state: String
145
- * user: User @hasInverse(field: "accounts")
146
- * }
147
- * type Session
148
- * @auth(
149
- * delete: { rule: "{$nextAuth: { eq: true } }" }
150
- * add: { rule: "{$nextAuth: { eq: true } }" }
151
- * query: { rule: "{$nextAuth: { eq: true } }" }
152
- * update: { rule: "{$nextAuth: { eq: true } }" }
153
- * ) {
154
- * id: ID
155
- * expires: DateTime
156
- * sessionToken: String @search(by: [hash])
157
- * user: User @hasInverse(field: "sessions")
158
- * }
159
- * type User
160
- * @auth(
161
- * query: {
162
- * or: [
163
- * {
164
- * rule: """
165
- * query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
166
- * """
167
- * }
168
- * { rule: "{$nextAuth: { eq: true } }" }
169
- * ]
170
- * }
171
- * delete: { rule: "{$nextAuth: { eq: true } }" }
172
- * add: { rule: "{$nextAuth: { eq: true } }" }
173
- * update: {
174
- * or: [
175
- * {
176
- * rule: """
177
- * query ($userId: String!) {queryUser(filter: { id: { eq: $userId } } ) {id}}
178
- * """
179
- * }
180
- * { rule: "{$nextAuth: { eq: true } }" }
181
- * ]
182
- * }
183
- * ) {
184
- * id: ID
185
- * name: String
186
- * email: String @search(by: [hash])
187
- * emailVerified: DateTime
188
- * image: String
189
- * accounts: [Account] @hasInverse(field: "user")
190
- * sessions: [Session] @hasInverse(field: "user")
191
- * }
192
- *
193
- * type VerificationToken
194
- * @auth(
195
- * delete: { rule: "{$nextAuth: { eq: true } }" }
196
- * add: { rule: "{$nextAuth: { eq: true } }" }
197
- * query: { rule: "{$nextAuth: { eq: true } }" }
198
- * update: { rule: "{$nextAuth: { eq: true } }" }
199
- * ) {
200
- * id: ID
201
- * identifier: String @search(by: [hash])
202
- * token: String @search(by: [hash])
203
- * expires: DateTime
204
- * }
205
- *
206
- * # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"<YOUR CUSTOM NAMESPACE HERE>","Algo":"HS256"}
207
- * ```
208
- *
209
- * ### Dgraph.Authorization
210
- *
211
- * In order to secure your graphql backend define the `Dgraph.Authorization` object at the
212
- * bottom of your schema and provide `authHeader` and `jwtSecret` values to the DgraphClient.
213
- *
214
- * ```js
215
- * # Dgraph.Authorization {"VerificationKey":"<YOUR JWT SECRET HERE>","Header":"<YOUR AUTH HEADER HERE>","Namespace":"YOUR CUSTOM NAMESPACE HERE","Algo":"HS256"}
216
- * ```
217
- *
218
- * ### VerificationKey and jwtSecret
219
- *
220
- * This is the key used to sign the JWT. Ex. `process.env.SECRET` or `process.env.APP_SECRET`.
221
- *
222
- * ### Header and authHeader
223
- *
224
- * The `Header` tells Dgraph where to lookup a JWT within the headers of the incoming requests made to the dgraph server.
225
- * You have to configure it at the bottom of your schema file. This header is the same as the `authHeader` property you
226
- * provide when you instantiate the `DgraphClient`.
227
- *
228
- * ### The nextAuth secret
229
- *
230
- * The `$nextAuth` secret is securely generated using the `jwtSecret` and injected by the DgraphAdapter in order to allow interacting with the JWT DgraphClient for anonymous user requests made within the system `ie. login, register`. This allows
231
- * secure interactions to be made with all the auth types required by next-auth. You have to specify it for each auth rule of
232
- * each type defined in your secure schema.
233
- *
234
- * ```js
235
- * type VerificationRequest
236
- * @auth(
237
- * delete: { rule: "{$nextAuth: { eq: true } }" },
238
- * add: { rule: "{$nextAuth: { eq: true } }" },
239
- * query: { rule: "{$nextAuth: { eq: true } }" },
240
- * update: { rule: "{$nextAuth: { eq: true } }" }
241
- * ) {
242
- * ...
243
- * }
244
- * ```
245
- *
246
- * ### JWT session and `@auth` directive
247
- *
248
- * Dgraph only works with HS256 or RS256 algorithms. If you want to use session jwt to securely interact with your dgraph
249
- * database you must customize next-auth `encode` and `decode` functions, as the default algorithm is HS512. You can
250
- * further customize the jwt with roles if you want to implement [`RBAC logic`](https://dgraph.io/docs/graphql/authorization/directive/#role-based-access-control).
251
- *
252
- * ```js
253
- * import * as jwt from "jsonwebtoken"
254
- * export default NextAuth({
255
- * session: {
256
- * strategy: "jwt",
257
- * },
258
- * jwt: {
259
- * secret: process.env.SECRET,
260
- * encode: async ({ secret, token }) => {
261
- * return jwt.sign({ ...token, userId: token.id }, secret, {
262
- * algorithm: "HS256",
263
- * expiresIn: 30 * 24 * 60 * 60, // 30 days
264
- * })
265
- * },
266
- * decode: async ({ secret, token }) => {
267
- * return jwt.verify(token, secret, { algorithms: ["HS256"] })
268
- * },
269
- * },
270
- * })
271
- * ```
272
- *
273
- * Once your `Dgraph.Authorization` is defined in your schema and the JWT settings are set, this will allow you to define
274
- * [`@auth rules`](https://dgraph.io/docs/graphql/authorization/authorization-overview/) for every part of your schema.
275
- **/
276
44
  export function DgraphAdapter(
277
45
  client: DgraphClientParams,
278
46
  options?: DgraphAdapterOptions