@opensaas/keystone-nextjs-auth 20.1.1 → 20.4.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/CHANGELOG.md +22 -0
- package/dist/declarations/src/lib/validateNextAuth.d.ts +1 -1
- package/dist/declarations/src/types.d.ts +1 -1
- package/dist/opensaas-keystone-nextjs-auth.cjs.dev.js +27 -2
- package/dist/opensaas-keystone-nextjs-auth.cjs.prod.js +27 -2
- package/dist/opensaas-keystone-nextjs-auth.esm.js +27 -2
- package/package.json +3 -3
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.dev.js +26 -25
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.prod.js +26 -25
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.esm.js +26 -25
- package/src/index.ts +20 -3
- package/src/lib/validateNextAuth.ts +0 -1
- package/src/pages/NextAuthPage.tsx +29 -29
- package/src/types.ts +1 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
# @opensaas-keystone/nextjs-auth
|
2
2
|
|
3
|
+
## 20.4.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- 80ba444: Add `getToken` to keystone `get session` to enable JWT in Authorization header
|
8
|
+
|
9
|
+
## 20.3.0
|
10
|
+
|
11
|
+
### Minor Changes
|
12
|
+
|
13
|
+
- 34e9932: Fix up stale next session - session is now refreshed on change of data
|
14
|
+
|
15
|
+
## 20.2.0
|
16
|
+
|
17
|
+
### Minor Changes
|
18
|
+
|
19
|
+
- 844f069: Generate NodeAPI by def, minor cleanups
|
20
|
+
|
21
|
+
### Patch Changes
|
22
|
+
|
23
|
+
- 6d63b1f: Minor Patch upgrades
|
24
|
+
|
3
25
|
## 20.1.1
|
4
26
|
|
5
27
|
### Patch Changes
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { KeystoneListsAPI } from '@keystone-6/core/types';
|
2
2
|
import { NextAuthErrorCode } from '../types';
|
3
|
-
export declare function validateNextAuth(
|
3
|
+
export declare function validateNextAuth(identityField: string, identity: string | number, protectIdentities: boolean, itemAPI: KeystoneListsAPI<any>[string]): Promise<{
|
4
4
|
success: false;
|
5
5
|
code: NextAuthErrorCode;
|
6
6
|
} | {
|
@@ -13,7 +13,7 @@ export declare type NextAuthSession = {
|
|
13
13
|
itemId: string;
|
14
14
|
data: any;
|
15
15
|
};
|
16
|
-
export declare type NextAuthProviders = [
|
16
|
+
export declare type NextAuthProviders = Provider[];
|
17
17
|
declare type KeytoneAuthProviders = {
|
18
18
|
providers: NextAuthProviders;
|
19
19
|
};
|
@@ -10,6 +10,7 @@ var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/string
|
|
10
10
|
var _URL = require('@babel/runtime-corejs3/core-js-stable/url');
|
11
11
|
var url = require('url');
|
12
12
|
var react = require('next-auth/react');
|
13
|
+
var jwt = require('next-auth/jwt');
|
13
14
|
var cookie = require('cookie');
|
14
15
|
var ejs = require('ejs');
|
15
16
|
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
@@ -213,7 +214,7 @@ const authTemplate = ({
|
|
213
214
|
return authOut;
|
214
215
|
};
|
215
216
|
|
216
|
-
const _excluded = ["get"];
|
217
|
+
const _excluded = ["get", "start"];
|
217
218
|
/**
|
218
219
|
* createAuth function
|
219
220
|
*
|
@@ -397,18 +398,39 @@ function createAuth({
|
|
397
398
|
|
398
399
|
|
399
400
|
const withItemData = _sessionStrategy => {
|
400
|
-
const
|
401
|
+
const {
|
402
|
+
get,
|
403
|
+
start
|
404
|
+
} = _sessionStrategy,
|
405
|
+
sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
|
401
406
|
|
402
407
|
return _objectSpread(_objectSpread({}, sessionStrategy), {}, {
|
408
|
+
start,
|
403
409
|
get: async ({
|
404
410
|
req
|
405
411
|
}) => {
|
412
|
+
var _req$headers$authoriz;
|
413
|
+
|
406
414
|
const pathname = url__default["default"].parse(req === null || req === void 0 ? void 0 : req.url).pathname;
|
407
415
|
|
408
416
|
if (_includesInstanceProperty__default["default"](pathname).call(pathname, '/api/auth')) {
|
409
417
|
return;
|
410
418
|
}
|
411
419
|
|
420
|
+
if (((_req$headers$authoriz = req.headers.authorization) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.split(' ')[0]) === 'Bearer') {
|
421
|
+
var _token$data;
|
422
|
+
|
423
|
+
const request = req;
|
424
|
+
const token = await jwt.getToken({
|
425
|
+
req: request,
|
426
|
+
secret: sessionSecret
|
427
|
+
});
|
428
|
+
|
429
|
+
if (token !== null && token !== void 0 && (_token$data = token.data) !== null && _token$data !== void 0 && _token$data.id) {
|
430
|
+
return token;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
|
412
434
|
const nextSession = await react.getSession({
|
413
435
|
req
|
414
436
|
});
|
@@ -490,6 +512,9 @@ function createAuth({
|
|
490
512
|
session,
|
491
513
|
providers,
|
492
514
|
lists: _objectSpread({}, keystoneConfig.lists),
|
515
|
+
experimental: _objectSpread(_objectSpread({}, keystoneConfig.experimental), {}, {
|
516
|
+
generateNodeAPI: true
|
517
|
+
}),
|
493
518
|
extendGraphqlSchema: existingExtendGraphQLSchema ? schema => existingExtendGraphQLSchema(extendGraphqlSchema(schema)) : extendGraphqlSchema
|
494
519
|
});
|
495
520
|
};
|
@@ -10,6 +10,7 @@ var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/string
|
|
10
10
|
var _URL = require('@babel/runtime-corejs3/core-js-stable/url');
|
11
11
|
var url = require('url');
|
12
12
|
var react = require('next-auth/react');
|
13
|
+
var jwt = require('next-auth/jwt');
|
13
14
|
var cookie = require('cookie');
|
14
15
|
var ejs = require('ejs');
|
15
16
|
var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter');
|
@@ -213,7 +214,7 @@ const authTemplate = ({
|
|
213
214
|
return authOut;
|
214
215
|
};
|
215
216
|
|
216
|
-
const _excluded = ["get"];
|
217
|
+
const _excluded = ["get", "start"];
|
217
218
|
/**
|
218
219
|
* createAuth function
|
219
220
|
*
|
@@ -397,18 +398,39 @@ function createAuth({
|
|
397
398
|
|
398
399
|
|
399
400
|
const withItemData = _sessionStrategy => {
|
400
|
-
const
|
401
|
+
const {
|
402
|
+
get,
|
403
|
+
start
|
404
|
+
} = _sessionStrategy,
|
405
|
+
sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
|
401
406
|
|
402
407
|
return _objectSpread(_objectSpread({}, sessionStrategy), {}, {
|
408
|
+
start,
|
403
409
|
get: async ({
|
404
410
|
req
|
405
411
|
}) => {
|
412
|
+
var _req$headers$authoriz;
|
413
|
+
|
406
414
|
const pathname = url__default["default"].parse(req === null || req === void 0 ? void 0 : req.url).pathname;
|
407
415
|
|
408
416
|
if (_includesInstanceProperty__default["default"](pathname).call(pathname, '/api/auth')) {
|
409
417
|
return;
|
410
418
|
}
|
411
419
|
|
420
|
+
if (((_req$headers$authoriz = req.headers.authorization) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.split(' ')[0]) === 'Bearer') {
|
421
|
+
var _token$data;
|
422
|
+
|
423
|
+
const request = req;
|
424
|
+
const token = await jwt.getToken({
|
425
|
+
req: request,
|
426
|
+
secret: sessionSecret
|
427
|
+
});
|
428
|
+
|
429
|
+
if (token !== null && token !== void 0 && (_token$data = token.data) !== null && _token$data !== void 0 && _token$data.id) {
|
430
|
+
return token;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
|
412
434
|
const nextSession = await react.getSession({
|
413
435
|
req
|
414
436
|
});
|
@@ -486,6 +508,9 @@ function createAuth({
|
|
486
508
|
session,
|
487
509
|
providers,
|
488
510
|
lists: _objectSpread({}, keystoneConfig.lists),
|
511
|
+
experimental: _objectSpread(_objectSpread({}, keystoneConfig.experimental), {}, {
|
512
|
+
generateNodeAPI: true
|
513
|
+
}),
|
489
514
|
extendGraphqlSchema: existingExtendGraphQLSchema ? schema => existingExtendGraphQLSchema(extendGraphqlSchema(schema)) : extendGraphqlSchema
|
490
515
|
});
|
491
516
|
};
|
@@ -6,6 +6,7 @@ import _JSON$stringify from '@babel/runtime-corejs3/core-js-stable/json/stringif
|
|
6
6
|
import _URL from '@babel/runtime-corejs3/core-js-stable/url';
|
7
7
|
import url from 'url';
|
8
8
|
import { getSession } from 'next-auth/react';
|
9
|
+
import { getToken } from 'next-auth/jwt';
|
9
10
|
import * as cookie from 'cookie';
|
10
11
|
import ejs from 'ejs';
|
11
12
|
import _filterInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/filter';
|
@@ -180,7 +181,7 @@ const authTemplate = ({
|
|
180
181
|
return authOut;
|
181
182
|
};
|
182
183
|
|
183
|
-
const _excluded = ["get"];
|
184
|
+
const _excluded = ["get", "start"];
|
184
185
|
/**
|
185
186
|
* createAuth function
|
186
187
|
*
|
@@ -364,18 +365,39 @@ function createAuth({
|
|
364
365
|
|
365
366
|
|
366
367
|
const withItemData = _sessionStrategy => {
|
367
|
-
const
|
368
|
+
const {
|
369
|
+
get,
|
370
|
+
start
|
371
|
+
} = _sessionStrategy,
|
372
|
+
sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
|
368
373
|
|
369
374
|
return _objectSpread(_objectSpread({}, sessionStrategy), {}, {
|
375
|
+
start,
|
370
376
|
get: async ({
|
371
377
|
req
|
372
378
|
}) => {
|
379
|
+
var _req$headers$authoriz;
|
380
|
+
|
373
381
|
const pathname = url.parse(req === null || req === void 0 ? void 0 : req.url).pathname;
|
374
382
|
|
375
383
|
if (_includesInstanceProperty(pathname).call(pathname, '/api/auth')) {
|
376
384
|
return;
|
377
385
|
}
|
378
386
|
|
387
|
+
if (((_req$headers$authoriz = req.headers.authorization) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.split(' ')[0]) === 'Bearer') {
|
388
|
+
var _token$data;
|
389
|
+
|
390
|
+
const request = req;
|
391
|
+
const token = await getToken({
|
392
|
+
req: request,
|
393
|
+
secret: sessionSecret
|
394
|
+
});
|
395
|
+
|
396
|
+
if (token !== null && token !== void 0 && (_token$data = token.data) !== null && _token$data !== void 0 && _token$data.id) {
|
397
|
+
return token;
|
398
|
+
}
|
399
|
+
}
|
400
|
+
|
379
401
|
const nextSession = await getSession({
|
380
402
|
req
|
381
403
|
});
|
@@ -457,6 +479,9 @@ function createAuth({
|
|
457
479
|
session,
|
458
480
|
providers,
|
459
481
|
lists: _objectSpread({}, keystoneConfig.lists),
|
482
|
+
experimental: _objectSpread(_objectSpread({}, keystoneConfig.experimental), {}, {
|
483
|
+
generateNodeAPI: true
|
484
|
+
}),
|
460
485
|
extendGraphqlSchema: existingExtendGraphQLSchema ? schema => existingExtendGraphQLSchema(extendGraphqlSchema(schema)) : extendGraphqlSchema
|
461
486
|
});
|
462
487
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@opensaas/keystone-nextjs-auth",
|
3
|
-
"version": "20.
|
3
|
+
"version": "20.4.0",
|
4
4
|
"repository": "https://github.com/opensaasau/keystone-nextjs-auth",
|
5
5
|
"license": "MIT",
|
6
6
|
"main": "dist/opensaas-keystone-nextjs-auth.cjs.js",
|
@@ -10,11 +10,11 @@
|
|
10
10
|
"@babel/runtime-corejs3": "^7.16.8",
|
11
11
|
"@types/ejs": "^3.1.0",
|
12
12
|
"cookie": "^0.4.1",
|
13
|
-
"cross-fetch": "^3.1.
|
13
|
+
"cross-fetch": "^3.1.5",
|
14
14
|
"ejs": "^3.1.6",
|
15
15
|
"fast-deep-equal": "^3.1.3",
|
16
16
|
"graphql": "^15.8.0",
|
17
|
-
"next-auth": "^4.1.
|
17
|
+
"next-auth": "^4.1.2"
|
18
18
|
},
|
19
19
|
"devDependencies": {
|
20
20
|
"@keystone-6/core": "^1.0.1",
|
@@ -37,7 +37,7 @@ async function findMatchingIdentity(identityField, identity, queryAPI) {
|
|
37
37
|
};
|
38
38
|
}
|
39
39
|
|
40
|
-
async function validateNextAuth(
|
40
|
+
async function validateNextAuth(identityField, identity, protectIdentities, itemAPI) {
|
41
41
|
const match = await findMatchingIdentity(identityField, identity, itemAPI);
|
42
42
|
const {
|
43
43
|
item
|
@@ -87,9 +87,11 @@ function NextAuthPage(props) {
|
|
87
87
|
identity = user.id;
|
88
88
|
} else if (typeof user.id === 'number') {
|
89
89
|
identity = user.id;
|
90
|
+
} else {
|
91
|
+
identity = 0;
|
90
92
|
}
|
91
93
|
|
92
|
-
const result = await validateNextAuth(
|
94
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
93
95
|
const data = {}; // eslint-disable-next-line no-restricted-syntax
|
94
96
|
|
95
97
|
for (const key in userMap) {
|
@@ -114,11 +116,11 @@ function NextAuthPage(props) {
|
|
114
116
|
|
115
117
|
if (!result.success) {
|
116
118
|
if (!autoCreate) {
|
117
|
-
console.log('
|
119
|
+
console.log('`autoCreate` if set to `false`, skipping user auto-creation');
|
118
120
|
return false;
|
119
121
|
}
|
120
122
|
|
121
|
-
console.log('
|
123
|
+
console.log('`autoCreate` if set to `true`, auto-creating a new user');
|
122
124
|
const createUser = await list.createOne({
|
123
125
|
data
|
124
126
|
}).then(returned => {
|
@@ -130,10 +132,10 @@ function NextAuthPage(props) {
|
|
130
132
|
});
|
131
133
|
console.log('Created User', createUser);
|
132
134
|
return createUser;
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
135
|
+
} // await list.updateOne({where: {id: result.item.id}, data});
|
136
|
+
|
137
|
+
|
138
|
+
return result.success;
|
137
139
|
},
|
138
140
|
|
139
141
|
async redirect({
|
@@ -162,30 +164,29 @@ function NextAuthPage(props) {
|
|
162
164
|
const identity = token.sub;
|
163
165
|
|
164
166
|
if (!token.itemId) {
|
165
|
-
const result = await validateNextAuth(
|
167
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
166
168
|
|
167
169
|
if (!result.success) {
|
168
|
-
return;
|
170
|
+
return token;
|
169
171
|
}
|
170
172
|
|
171
|
-
|
172
|
-
|
173
|
-
id: result.item.id
|
174
|
-
},
|
175
|
-
query: sessionData || 'id'
|
176
|
-
});
|
173
|
+
token.itemId = result.item.id;
|
174
|
+
}
|
177
175
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
176
|
+
const data = await query[listKey].findOne({
|
177
|
+
where: {
|
178
|
+
id: token.itemId
|
179
|
+
},
|
180
|
+
query: sessionData || 'id'
|
181
|
+
});
|
184
182
|
|
185
|
-
|
186
|
-
|
183
|
+
const returnToken = _objectSpread(_objectSpread({}, token), {}, {
|
184
|
+
data,
|
185
|
+
subject: token.sub,
|
186
|
+
listKey
|
187
|
+
});
|
187
188
|
|
188
|
-
return
|
189
|
+
return returnToken;
|
189
190
|
}
|
190
191
|
|
191
192
|
}
|
package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.prod.js
CHANGED
@@ -37,7 +37,7 @@ async function findMatchingIdentity(identityField, identity, queryAPI) {
|
|
37
37
|
};
|
38
38
|
}
|
39
39
|
|
40
|
-
async function validateNextAuth(
|
40
|
+
async function validateNextAuth(identityField, identity, protectIdentities, itemAPI) {
|
41
41
|
const match = await findMatchingIdentity(identityField, identity, itemAPI);
|
42
42
|
const {
|
43
43
|
item
|
@@ -87,9 +87,11 @@ function NextAuthPage(props) {
|
|
87
87
|
identity = user.id;
|
88
88
|
} else if (typeof user.id === 'number') {
|
89
89
|
identity = user.id;
|
90
|
+
} else {
|
91
|
+
identity = 0;
|
90
92
|
}
|
91
93
|
|
92
|
-
const result = await validateNextAuth(
|
94
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
93
95
|
const data = {}; // eslint-disable-next-line no-restricted-syntax
|
94
96
|
|
95
97
|
for (const key in userMap) {
|
@@ -114,11 +116,11 @@ function NextAuthPage(props) {
|
|
114
116
|
|
115
117
|
if (!result.success) {
|
116
118
|
if (!autoCreate) {
|
117
|
-
console.log('
|
119
|
+
console.log('`autoCreate` if set to `false`, skipping user auto-creation');
|
118
120
|
return false;
|
119
121
|
}
|
120
122
|
|
121
|
-
console.log('
|
123
|
+
console.log('`autoCreate` if set to `true`, auto-creating a new user');
|
122
124
|
const createUser = await list.createOne({
|
123
125
|
data
|
124
126
|
}).then(returned => {
|
@@ -130,10 +132,10 @@ function NextAuthPage(props) {
|
|
130
132
|
});
|
131
133
|
console.log('Created User', createUser);
|
132
134
|
return createUser;
|
133
|
-
}
|
134
|
-
|
135
|
-
|
136
|
-
|
135
|
+
} // await list.updateOne({where: {id: result.item.id}, data});
|
136
|
+
|
137
|
+
|
138
|
+
return result.success;
|
137
139
|
},
|
138
140
|
|
139
141
|
async redirect({
|
@@ -162,30 +164,29 @@ function NextAuthPage(props) {
|
|
162
164
|
const identity = token.sub;
|
163
165
|
|
164
166
|
if (!token.itemId) {
|
165
|
-
const result = await validateNextAuth(
|
167
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
166
168
|
|
167
169
|
if (!result.success) {
|
168
|
-
return;
|
170
|
+
return token;
|
169
171
|
}
|
170
172
|
|
171
|
-
|
172
|
-
|
173
|
-
id: result.item.id
|
174
|
-
},
|
175
|
-
query: sessionData || 'id'
|
176
|
-
});
|
173
|
+
token.itemId = result.item.id;
|
174
|
+
}
|
177
175
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
176
|
+
const data = await query[listKey].findOne({
|
177
|
+
where: {
|
178
|
+
id: token.itemId
|
179
|
+
},
|
180
|
+
query: sessionData || 'id'
|
181
|
+
});
|
184
182
|
|
185
|
-
|
186
|
-
|
183
|
+
const returnToken = _objectSpread(_objectSpread({}, token), {}, {
|
184
|
+
data,
|
185
|
+
subject: token.sub,
|
186
|
+
listKey
|
187
|
+
});
|
187
188
|
|
188
|
-
return
|
189
|
+
return returnToken;
|
189
190
|
}
|
190
191
|
|
191
192
|
}
|
@@ -28,7 +28,7 @@ async function findMatchingIdentity(identityField, identity, queryAPI) {
|
|
28
28
|
};
|
29
29
|
}
|
30
30
|
|
31
|
-
async function validateNextAuth(
|
31
|
+
async function validateNextAuth(identityField, identity, protectIdentities, itemAPI) {
|
32
32
|
const match = await findMatchingIdentity(identityField, identity, itemAPI);
|
33
33
|
const {
|
34
34
|
item
|
@@ -78,9 +78,11 @@ function NextAuthPage(props) {
|
|
78
78
|
identity = user.id;
|
79
79
|
} else if (typeof user.id === 'number') {
|
80
80
|
identity = user.id;
|
81
|
+
} else {
|
82
|
+
identity = 0;
|
81
83
|
}
|
82
84
|
|
83
|
-
const result = await validateNextAuth(
|
85
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
84
86
|
const data = {}; // eslint-disable-next-line no-restricted-syntax
|
85
87
|
|
86
88
|
for (const key in userMap) {
|
@@ -105,11 +107,11 @@ function NextAuthPage(props) {
|
|
105
107
|
|
106
108
|
if (!result.success) {
|
107
109
|
if (!autoCreate) {
|
108
|
-
console.log('
|
110
|
+
console.log('`autoCreate` if set to `false`, skipping user auto-creation');
|
109
111
|
return false;
|
110
112
|
}
|
111
113
|
|
112
|
-
console.log('
|
114
|
+
console.log('`autoCreate` if set to `true`, auto-creating a new user');
|
113
115
|
const createUser = await list.createOne({
|
114
116
|
data
|
115
117
|
}).then(returned => {
|
@@ -121,10 +123,10 @@ function NextAuthPage(props) {
|
|
121
123
|
});
|
122
124
|
console.log('Created User', createUser);
|
123
125
|
return createUser;
|
124
|
-
}
|
125
|
-
|
126
|
-
|
127
|
-
|
126
|
+
} // await list.updateOne({where: {id: result.item.id}, data});
|
127
|
+
|
128
|
+
|
129
|
+
return result.success;
|
128
130
|
},
|
129
131
|
|
130
132
|
async redirect({
|
@@ -153,30 +155,29 @@ function NextAuthPage(props) {
|
|
153
155
|
const identity = token.sub;
|
154
156
|
|
155
157
|
if (!token.itemId) {
|
156
|
-
const result = await validateNextAuth(
|
158
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
157
159
|
|
158
160
|
if (!result.success) {
|
159
|
-
return;
|
161
|
+
return token;
|
160
162
|
}
|
161
163
|
|
162
|
-
|
163
|
-
|
164
|
-
id: result.item.id
|
165
|
-
},
|
166
|
-
query: sessionData || 'id'
|
167
|
-
});
|
164
|
+
token.itemId = result.item.id;
|
165
|
+
}
|
168
166
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
167
|
+
const data = await query[listKey].findOne({
|
168
|
+
where: {
|
169
|
+
id: token.itemId
|
170
|
+
},
|
171
|
+
query: sessionData || 'id'
|
172
|
+
});
|
175
173
|
|
176
|
-
|
177
|
-
|
174
|
+
const returnToken = _objectSpread(_objectSpread({}, token), {}, {
|
175
|
+
data,
|
176
|
+
subject: token.sub,
|
177
|
+
listKey
|
178
|
+
});
|
178
179
|
|
179
|
-
return
|
180
|
+
return returnToken;
|
180
181
|
}
|
181
182
|
|
182
183
|
}
|
package/src/index.ts
CHANGED
@@ -9,7 +9,10 @@ import {
|
|
9
9
|
BaseKeystoneTypeInfo,
|
10
10
|
} from '@keystone-6/core/types';
|
11
11
|
import { getSession } from 'next-auth/react';
|
12
|
+
import { getToken } from 'next-auth/jwt';
|
12
13
|
import * as cookie from 'cookie';
|
14
|
+
import { Provider } from 'next-auth/providers';
|
15
|
+
import { NextApiRequest } from 'next';
|
13
16
|
import { nextConfigTemplate } from './templates/next-config';
|
14
17
|
// import * as Path from 'path';
|
15
18
|
|
@@ -136,7 +139,7 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
|
|
136
139
|
`${customPath}/api/auth/providers`,
|
137
140
|
`${customPath}/api/auth/signout`,
|
138
141
|
];
|
139
|
-
function addPages(provider) {
|
142
|
+
function addPages(provider: Provider) {
|
140
143
|
const name = provider.id;
|
141
144
|
publicPages.push(`${customPath}/api/auth/signin/${name}`);
|
142
145
|
publicPages.push(`${customPath}/api/auth/callback/${name}`);
|
@@ -188,15 +191,25 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
|
|
188
191
|
const withItemData = (
|
189
192
|
_sessionStrategy: SessionStrategy<Record<string, any>>
|
190
193
|
): SessionStrategy<NextAuthSession | undefined> => {
|
191
|
-
const { get, ...sessionStrategy } = _sessionStrategy;
|
194
|
+
const { get, start, ...sessionStrategy } = _sessionStrategy;
|
192
195
|
return {
|
193
196
|
...sessionStrategy,
|
197
|
+
start,
|
194
198
|
get: async ({ req }) => {
|
195
199
|
const pathname = url.parse(req?.url!).pathname!;
|
196
200
|
if (pathname.includes('/api/auth')) {
|
197
201
|
return;
|
198
202
|
}
|
203
|
+
if (req.headers.authorization?.split(' ')[0] === 'Bearer') {
|
204
|
+
const request = req as NextApiRequest;
|
205
|
+
const token = await getToken({ req: request, secret: sessionSecret });
|
206
|
+
|
207
|
+
if (token?.data?.id) {
|
208
|
+
return token as NextAuthSession;
|
209
|
+
}
|
210
|
+
}
|
199
211
|
const nextSession: unknown = await getSession({ req });
|
212
|
+
|
200
213
|
if (nextSession) {
|
201
214
|
return nextSession as NextAuthSession;
|
202
215
|
}
|
@@ -215,7 +228,7 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
|
|
215
228
|
secure: process.env.NODE_ENV === 'production',
|
216
229
|
path: '/',
|
217
230
|
sameSite: 'lax',
|
218
|
-
domain: url.parse(req.url).hostname,
|
231
|
+
domain: url.parse(req.url as string).hostname as string,
|
219
232
|
})
|
220
233
|
);
|
221
234
|
},
|
@@ -292,6 +305,10 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
|
|
292
305
|
lists: {
|
293
306
|
...keystoneConfig.lists,
|
294
307
|
},
|
308
|
+
experimental: {
|
309
|
+
...keystoneConfig.experimental,
|
310
|
+
generateNodeAPI: true,
|
311
|
+
},
|
295
312
|
extendGraphqlSchema: existingExtendGraphQLSchema
|
296
313
|
? (schema) => existingExtendGraphQLSchema(extendGraphqlSchema(schema))
|
297
314
|
: extendGraphqlSchema,
|
@@ -45,15 +45,16 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
45
45
|
identity = user.id;
|
46
46
|
} else if (typeof user.id === 'number') {
|
47
47
|
identity = user.id;
|
48
|
+
} else {
|
49
|
+
identity = 0;
|
48
50
|
}
|
49
51
|
const result = await validateNextAuth(
|
50
|
-
list,
|
51
52
|
identityField,
|
52
53
|
identity,
|
53
54
|
protectIdentities,
|
54
55
|
queryAPI
|
55
56
|
);
|
56
|
-
const data = {};
|
57
|
+
const data: any = {};
|
57
58
|
// eslint-disable-next-line no-restricted-syntax
|
58
59
|
for (const key in userMap) {
|
59
60
|
if (Object.prototype.hasOwnProperty.call(userMap, key)) {
|
@@ -75,12 +76,16 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
75
76
|
|
76
77
|
if (!result.success) {
|
77
78
|
if (!autoCreate) {
|
78
|
-
console.log(
|
79
|
+
console.log(
|
80
|
+
'`autoCreate` if set to `false`, skipping user auto-creation'
|
81
|
+
);
|
79
82
|
return false;
|
80
83
|
}
|
81
|
-
console.log(
|
84
|
+
console.log(
|
85
|
+
'`autoCreate` if set to `true`, auto-creating a new user'
|
86
|
+
);
|
82
87
|
|
83
|
-
const createUser =await list
|
88
|
+
const createUser = await list
|
84
89
|
.createOne({ data })
|
85
90
|
.then((returned) => {
|
86
91
|
console.log('User Created', JSON.stringify(returned));
|
@@ -90,13 +95,11 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
90
95
|
console.log(error);
|
91
96
|
throw new Error(error);
|
92
97
|
});
|
93
|
-
|
94
|
-
|
95
|
-
} else {
|
96
|
-
// await list.updateOne({where: {id: result.item.id}, data});
|
97
|
-
return result.success;
|
98
|
+
console.log('Created User', createUser);
|
99
|
+
return createUser;
|
98
100
|
}
|
99
|
-
|
101
|
+
// await list.updateOne({where: {id: result.item.id}, data});
|
102
|
+
return result.success;
|
100
103
|
},
|
101
104
|
async redirect({ url }) {
|
102
105
|
return url;
|
@@ -112,10 +115,9 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
112
115
|
return returnSession;
|
113
116
|
},
|
114
117
|
async jwt({ token }) {
|
115
|
-
const identity = token.sub;
|
118
|
+
const identity = token.sub as number | string;
|
116
119
|
if (!token.itemId) {
|
117
120
|
const result = await validateNextAuth(
|
118
|
-
list,
|
119
121
|
identityField,
|
120
122
|
identity,
|
121
123
|
protectIdentities,
|
@@ -123,24 +125,22 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
123
125
|
);
|
124
126
|
|
125
127
|
if (!result.success) {
|
126
|
-
return;
|
128
|
+
return token;
|
127
129
|
}
|
128
|
-
|
129
|
-
const data = await query[listKey].findOne({
|
130
|
-
where: { id: result.item.id },
|
131
|
-
query: sessionData || 'id',
|
132
|
-
});
|
133
|
-
const returnToken = {
|
134
|
-
...token,
|
135
|
-
data,
|
136
|
-
subject: token.sub,
|
137
|
-
listKey,
|
138
|
-
itemId: result.item.id.toString(),
|
139
|
-
};
|
140
|
-
|
141
|
-
return returnToken;
|
130
|
+
token.itemId = result.item.id;
|
142
131
|
}
|
143
|
-
|
132
|
+
const data = await query[listKey].findOne({
|
133
|
+
where: { id: token.itemId },
|
134
|
+
query: sessionData || 'id',
|
135
|
+
});
|
136
|
+
const returnToken = {
|
137
|
+
...token,
|
138
|
+
data,
|
139
|
+
subject: token.sub,
|
140
|
+
listKey,
|
141
|
+
};
|
142
|
+
|
143
|
+
return returnToken;
|
144
144
|
},
|
145
145
|
},
|
146
146
|
});
|
package/src/types.ts
CHANGED
@@ -12,7 +12,7 @@ export type AuthGqlNames = {
|
|
12
12
|
|
13
13
|
export type NextAuthSession = { listKey: string; itemId: string; data: any };
|
14
14
|
|
15
|
-
export type NextAuthProviders = [
|
15
|
+
export type NextAuthProviders = Provider[];
|
16
16
|
|
17
17
|
type KeytoneAuthProviders = {
|
18
18
|
providers: NextAuthProviders;
|