@opensaas/keystone-nextjs-auth 22.1.0 → 22.2.2
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/pages/NextAuthPage.d.ts +13 -14
- package/dist/declarations/src/templates/auth.d.ts +2 -4
- package/dist/opensaas-keystone-nextjs-auth.cjs.dev.js +29 -12
- package/dist/opensaas-keystone-nextjs-auth.cjs.prod.js +29 -12
- package/dist/opensaas-keystone-nextjs-auth.esm.js +29 -12
- package/package.json +1 -1
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.dev.js +4 -10
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.prod.js +4 -10
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.esm.js +4 -10
- package/src/index.ts +7 -6
- package/src/pages/NextAuthPage.tsx +18 -24
- package/src/templates/auth.ts +9 -6
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
# @opensaas-keystone/nextjs-auth
|
2
2
|
|
3
|
+
## 22.2.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 544ffd8: fix session order
|
8
|
+
|
9
|
+
## 22.2.1
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 8599b89: remove global prisma client as it is not required
|
14
|
+
|
15
|
+
## 22.2.0
|
16
|
+
|
17
|
+
### Minor Changes
|
18
|
+
|
19
|
+
- 4a2f20a: Allow custom session `get`, `start` and `end`
|
20
|
+
|
21
|
+
### Patch Changes
|
22
|
+
|
23
|
+
- 4a2f20a: Set prisma and query on global in dev to prevent instantiating extra `PrismaClient` instances
|
24
|
+
|
3
25
|
## 22.1.0
|
4
26
|
|
5
27
|
### Minor Changes
|
@@ -2,30 +2,29 @@ import { CookiesOptions, EventCallbacks, PagesOptions } from 'next-auth';
|
|
2
2
|
import type { KeystoneListsAPI } from '@keystone-6/core/types';
|
3
3
|
import { Provider } from 'next-auth/providers';
|
4
4
|
import { JWTOptions } from 'next-auth/jwt';
|
5
|
-
declare type
|
5
|
+
export declare type NextAuthTemplateProps = {
|
6
6
|
autoCreate: boolean;
|
7
|
+
identityField: string;
|
8
|
+
listKey: string;
|
9
|
+
sessionData: string | undefined;
|
10
|
+
sessionSecret: string;
|
11
|
+
};
|
12
|
+
export declare type CoreNextAuthPageProps = {
|
7
13
|
cookies?: Partial<CookiesOptions>;
|
8
14
|
events?: Partial<EventCallbacks>;
|
9
|
-
identityField: string;
|
10
15
|
jwt?: Partial<JWTOptions>;
|
11
|
-
listKey: string;
|
12
16
|
pages?: Partial<PagesOptions>;
|
13
|
-
providers
|
17
|
+
providers: Provider[];
|
14
18
|
resolver?: (args: {
|
15
19
|
user: any;
|
16
20
|
profile: any;
|
17
21
|
account: any;
|
18
|
-
}) => {
|
22
|
+
}) => Promise<{
|
19
23
|
[key: string]: boolean | string | number;
|
20
|
-
}
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
declare type NextAuthGglProps = {
|
25
|
-
mutationName?: string;
|
26
|
-
query?: KeystoneListsAPI<any>;
|
24
|
+
}>;
|
25
|
+
} & NextAuthTemplateProps;
|
26
|
+
export declare type NextAuthPageProps = CoreNextAuthPageProps & {
|
27
|
+
query: KeystoneListsAPI<any>;
|
27
28
|
};
|
28
|
-
export declare type NextAuthPageProps = CoreNextAuthPageProps & NextAuthGglProps;
|
29
29
|
export default function NextAuthPage(props: NextAuthPageProps): any;
|
30
30
|
export declare const getNextAuthPage: (props: NextAuthPageProps) => () => any;
|
31
|
-
export {};
|
@@ -1,4 +1,2 @@
|
|
1
|
-
import {
|
2
|
-
declare
|
3
|
-
export declare const authTemplate: ({ autoCreate, identityField, listKey, sessionData, sessionSecret, }: AuthTemplateOptions) => string;
|
4
|
-
export {};
|
1
|
+
import { NextAuthTemplateProps } from '../pages/NextAuthPage';
|
2
|
+
export declare const authTemplate: ({ autoCreate, identityField, listKey, sessionData, sessionSecret, }: NextAuthTemplateProps) => string;
|
@@ -164,8 +164,13 @@ const getSchemaExtension = ({
|
|
164
164
|
|
165
165
|
const template = `
|
166
166
|
import getNextAuthPage from '@opensaas/keystone-nextjs-auth/pages/NextAuthPage';
|
167
|
-
import { query } from '.keystone/api';
|
168
167
|
import keystoneConfig from '../../../../../keystone';
|
168
|
+
import { PrismaClient } from '.prisma/client';
|
169
|
+
import { createQueryAPI } from '@keystone-6/core/___internal-do-not-use-will-break-in-patch/node-api';
|
170
|
+
|
171
|
+
const keystoneQueryAPI = global.keystoneQueryAPI || createQueryAPI(keystoneConfig, PrismaClient);
|
172
|
+
|
173
|
+
if (process.env.NODE_ENV !== 'production') globalThis.keystoneQueryAPI = keystoneQueryAPI
|
169
174
|
|
170
175
|
export default getNextAuthPage({
|
171
176
|
autoCreate: <%= autoCreate %>,
|
@@ -173,7 +178,7 @@ export default getNextAuthPage({
|
|
173
178
|
listKey: '<%= listKey %>',
|
174
179
|
pages: keystoneConfig.pages,
|
175
180
|
providers: keystoneConfig.providers,
|
176
|
-
query,
|
181
|
+
query: keystoneQueryAPI,
|
177
182
|
resolver: keystoneConfig.resolver,
|
178
183
|
sessionData: '<%= sessionData %>',
|
179
184
|
sessionSecret: '<%= sessionSecret %>',
|
@@ -196,7 +201,7 @@ const authTemplate = ({
|
|
196
201
|
return authOut;
|
197
202
|
};
|
198
203
|
|
199
|
-
const _excluded = ["get"];
|
204
|
+
const _excluded = ["get", "end"];
|
200
205
|
/**
|
201
206
|
* createAuth function
|
202
207
|
*
|
@@ -368,21 +373,19 @@ function createAuth({
|
|
368
373
|
|
369
374
|
|
370
375
|
const withItemData = _sessionStrategy => {
|
371
|
-
const
|
376
|
+
const {
|
377
|
+
get,
|
378
|
+
end
|
379
|
+
} = _sessionStrategy,
|
380
|
+
sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
|
372
381
|
|
373
382
|
return _objectSpread(_objectSpread({}, sessionStrategy), {}, {
|
374
|
-
start: async () => {
|
375
|
-
return 'false';
|
376
|
-
},
|
377
383
|
get: async ({
|
378
384
|
req,
|
379
385
|
createContext
|
380
386
|
}) => {
|
381
387
|
var _req$headers, _req$headers$authoriz;
|
382
388
|
|
383
|
-
const sudoContext = createContext({
|
384
|
-
sudo: true
|
385
|
-
});
|
386
389
|
const pathname = url__default["default"].parse(req === null || req === void 0 ? void 0 : req.url).pathname;
|
387
390
|
let nextSession;
|
388
391
|
|
@@ -390,6 +393,14 @@ function createAuth({
|
|
390
393
|
return;
|
391
394
|
}
|
392
395
|
|
396
|
+
const sudoContext = createContext({
|
397
|
+
sudo: true
|
398
|
+
});
|
399
|
+
const session = await get({
|
400
|
+
req,
|
401
|
+
createContext
|
402
|
+
});
|
403
|
+
|
393
404
|
if (((_req$headers = req.headers) === null || _req$headers === void 0 ? void 0 : (_req$headers$authoriz = _req$headers.authorization) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.split(' ')[0]) === 'Bearer') {
|
394
405
|
nextSession = await jwt.getToken({
|
395
406
|
req,
|
@@ -405,7 +416,7 @@ function createAuth({
|
|
405
416
|
return;
|
406
417
|
}
|
407
418
|
|
408
|
-
return _objectSpread(_objectSpread({}, nextSession), {}, {
|
419
|
+
return _objectSpread(_objectSpread(_objectSpread({}, session), nextSession), {}, {
|
409
420
|
data: nextSession.data,
|
410
421
|
listKey: nextSession.listKey,
|
411
422
|
itemId: nextSession.itemId
|
@@ -413,8 +424,14 @@ function createAuth({
|
|
413
424
|
},
|
414
425
|
end: async ({
|
415
426
|
res,
|
416
|
-
req
|
427
|
+
req,
|
428
|
+
createContext
|
417
429
|
}) => {
|
430
|
+
await end({
|
431
|
+
res,
|
432
|
+
req,
|
433
|
+
createContext
|
434
|
+
});
|
418
435
|
const TOKEN_NAME = process.env.NODE_ENV === 'production' ? '__Secure-next-auth.session-token' : 'next-auth.session-token';
|
419
436
|
res.setHeader('Set-Cookie', cookie__namespace.serialize(TOKEN_NAME, '', {
|
420
437
|
maxAge: 0,
|
@@ -163,8 +163,13 @@ const getSchemaExtension = ({
|
|
163
163
|
|
164
164
|
const template = `
|
165
165
|
import getNextAuthPage from '@opensaas/keystone-nextjs-auth/pages/NextAuthPage';
|
166
|
-
import { query } from '.keystone/api';
|
167
166
|
import keystoneConfig from '../../../../../keystone';
|
167
|
+
import { PrismaClient } from '.prisma/client';
|
168
|
+
import { createQueryAPI } from '@keystone-6/core/___internal-do-not-use-will-break-in-patch/node-api';
|
169
|
+
|
170
|
+
const keystoneQueryAPI = global.keystoneQueryAPI || createQueryAPI(keystoneConfig, PrismaClient);
|
171
|
+
|
172
|
+
if (process.env.NODE_ENV !== 'production') globalThis.keystoneQueryAPI = keystoneQueryAPI
|
168
173
|
|
169
174
|
export default getNextAuthPage({
|
170
175
|
autoCreate: <%= autoCreate %>,
|
@@ -172,7 +177,7 @@ export default getNextAuthPage({
|
|
172
177
|
listKey: '<%= listKey %>',
|
173
178
|
pages: keystoneConfig.pages,
|
174
179
|
providers: keystoneConfig.providers,
|
175
|
-
query,
|
180
|
+
query: keystoneQueryAPI,
|
176
181
|
resolver: keystoneConfig.resolver,
|
177
182
|
sessionData: '<%= sessionData %>',
|
178
183
|
sessionSecret: '<%= sessionSecret %>',
|
@@ -195,7 +200,7 @@ const authTemplate = ({
|
|
195
200
|
return authOut;
|
196
201
|
};
|
197
202
|
|
198
|
-
const _excluded = ["get"];
|
203
|
+
const _excluded = ["get", "end"];
|
199
204
|
/**
|
200
205
|
* createAuth function
|
201
206
|
*
|
@@ -367,21 +372,19 @@ function createAuth({
|
|
367
372
|
|
368
373
|
|
369
374
|
const withItemData = _sessionStrategy => {
|
370
|
-
const
|
375
|
+
const {
|
376
|
+
get,
|
377
|
+
end
|
378
|
+
} = _sessionStrategy,
|
379
|
+
sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
|
371
380
|
|
372
381
|
return _objectSpread(_objectSpread({}, sessionStrategy), {}, {
|
373
|
-
start: async () => {
|
374
|
-
return 'false';
|
375
|
-
},
|
376
382
|
get: async ({
|
377
383
|
req,
|
378
384
|
createContext
|
379
385
|
}) => {
|
380
386
|
var _req$headers, _req$headers$authoriz;
|
381
387
|
|
382
|
-
const sudoContext = createContext({
|
383
|
-
sudo: true
|
384
|
-
});
|
385
388
|
const pathname = url__default["default"].parse(req === null || req === void 0 ? void 0 : req.url).pathname;
|
386
389
|
let nextSession;
|
387
390
|
|
@@ -389,6 +392,14 @@ function createAuth({
|
|
389
392
|
return;
|
390
393
|
}
|
391
394
|
|
395
|
+
const sudoContext = createContext({
|
396
|
+
sudo: true
|
397
|
+
});
|
398
|
+
const session = await get({
|
399
|
+
req,
|
400
|
+
createContext
|
401
|
+
});
|
402
|
+
|
392
403
|
if (((_req$headers = req.headers) === null || _req$headers === void 0 ? void 0 : (_req$headers$authoriz = _req$headers.authorization) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.split(' ')[0]) === 'Bearer') {
|
393
404
|
nextSession = await jwt.getToken({
|
394
405
|
req,
|
@@ -404,7 +415,7 @@ function createAuth({
|
|
404
415
|
return;
|
405
416
|
}
|
406
417
|
|
407
|
-
return _objectSpread(_objectSpread({}, nextSession), {}, {
|
418
|
+
return _objectSpread(_objectSpread(_objectSpread({}, session), nextSession), {}, {
|
408
419
|
data: nextSession.data,
|
409
420
|
listKey: nextSession.listKey,
|
410
421
|
itemId: nextSession.itemId
|
@@ -412,8 +423,14 @@ function createAuth({
|
|
412
423
|
},
|
413
424
|
end: async ({
|
414
425
|
res,
|
415
|
-
req
|
426
|
+
req,
|
427
|
+
createContext
|
416
428
|
}) => {
|
429
|
+
await end({
|
430
|
+
res,
|
431
|
+
req,
|
432
|
+
createContext
|
433
|
+
});
|
417
434
|
const TOKEN_NAME = '__Secure-next-auth.session-token' ;
|
418
435
|
res.setHeader('Set-Cookie', cookie__namespace.serialize(TOKEN_NAME, '', {
|
419
436
|
maxAge: 0,
|
@@ -131,8 +131,13 @@ const getSchemaExtension = ({
|
|
131
131
|
|
132
132
|
const template = `
|
133
133
|
import getNextAuthPage from '@opensaas/keystone-nextjs-auth/pages/NextAuthPage';
|
134
|
-
import { query } from '.keystone/api';
|
135
134
|
import keystoneConfig from '../../../../../keystone';
|
135
|
+
import { PrismaClient } from '.prisma/client';
|
136
|
+
import { createQueryAPI } from '@keystone-6/core/___internal-do-not-use-will-break-in-patch/node-api';
|
137
|
+
|
138
|
+
const keystoneQueryAPI = global.keystoneQueryAPI || createQueryAPI(keystoneConfig, PrismaClient);
|
139
|
+
|
140
|
+
if (process.env.NODE_ENV !== 'production') globalThis.keystoneQueryAPI = keystoneQueryAPI
|
136
141
|
|
137
142
|
export default getNextAuthPage({
|
138
143
|
autoCreate: <%= autoCreate %>,
|
@@ -140,7 +145,7 @@ export default getNextAuthPage({
|
|
140
145
|
listKey: '<%= listKey %>',
|
141
146
|
pages: keystoneConfig.pages,
|
142
147
|
providers: keystoneConfig.providers,
|
143
|
-
query,
|
148
|
+
query: keystoneQueryAPI,
|
144
149
|
resolver: keystoneConfig.resolver,
|
145
150
|
sessionData: '<%= sessionData %>',
|
146
151
|
sessionSecret: '<%= sessionSecret %>',
|
@@ -163,7 +168,7 @@ const authTemplate = ({
|
|
163
168
|
return authOut;
|
164
169
|
};
|
165
170
|
|
166
|
-
const _excluded = ["get"];
|
171
|
+
const _excluded = ["get", "end"];
|
167
172
|
/**
|
168
173
|
* createAuth function
|
169
174
|
*
|
@@ -335,21 +340,19 @@ function createAuth({
|
|
335
340
|
|
336
341
|
|
337
342
|
const withItemData = _sessionStrategy => {
|
338
|
-
const
|
343
|
+
const {
|
344
|
+
get,
|
345
|
+
end
|
346
|
+
} = _sessionStrategy,
|
347
|
+
sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
|
339
348
|
|
340
349
|
return _objectSpread(_objectSpread({}, sessionStrategy), {}, {
|
341
|
-
start: async () => {
|
342
|
-
return 'false';
|
343
|
-
},
|
344
350
|
get: async ({
|
345
351
|
req,
|
346
352
|
createContext
|
347
353
|
}) => {
|
348
354
|
var _req$headers, _req$headers$authoriz;
|
349
355
|
|
350
|
-
const sudoContext = createContext({
|
351
|
-
sudo: true
|
352
|
-
});
|
353
356
|
const pathname = url.parse(req === null || req === void 0 ? void 0 : req.url).pathname;
|
354
357
|
let nextSession;
|
355
358
|
|
@@ -357,6 +360,14 @@ function createAuth({
|
|
357
360
|
return;
|
358
361
|
}
|
359
362
|
|
363
|
+
const sudoContext = createContext({
|
364
|
+
sudo: true
|
365
|
+
});
|
366
|
+
const session = await get({
|
367
|
+
req,
|
368
|
+
createContext
|
369
|
+
});
|
370
|
+
|
360
371
|
if (((_req$headers = req.headers) === null || _req$headers === void 0 ? void 0 : (_req$headers$authoriz = _req$headers.authorization) === null || _req$headers$authoriz === void 0 ? void 0 : _req$headers$authoriz.split(' ')[0]) === 'Bearer') {
|
361
372
|
nextSession = await getToken({
|
362
373
|
req,
|
@@ -372,7 +383,7 @@ function createAuth({
|
|
372
383
|
return;
|
373
384
|
}
|
374
385
|
|
375
|
-
return _objectSpread(_objectSpread({}, nextSession), {}, {
|
386
|
+
return _objectSpread(_objectSpread(_objectSpread({}, session), nextSession), {}, {
|
376
387
|
data: nextSession.data,
|
377
388
|
listKey: nextSession.listKey,
|
378
389
|
itemId: nextSession.itemId
|
@@ -380,8 +391,14 @@ function createAuth({
|
|
380
391
|
},
|
381
392
|
end: async ({
|
382
393
|
res,
|
383
|
-
req
|
394
|
+
req,
|
395
|
+
createContext
|
384
396
|
}) => {
|
397
|
+
await end({
|
398
|
+
res,
|
399
|
+
req,
|
400
|
+
createContext
|
401
|
+
});
|
385
402
|
const TOKEN_NAME = process.env.NODE_ENV === 'production' ? '__Secure-next-auth.session-token' : 'next-auth.session-token';
|
386
403
|
res.setHeader('Set-Cookie', cookie.serialize(TOKEN_NAME, '', {
|
387
404
|
maxAge: 0,
|
package/package.json
CHANGED
@@ -81,7 +81,6 @@ function NextAuthPage(props) {
|
|
81
81
|
}
|
82
82
|
|
83
83
|
const list = query[listKey];
|
84
|
-
const queryAPI = query[listKey];
|
85
84
|
const protectIdentities = true;
|
86
85
|
return NextAuth__default["default"]({
|
87
86
|
cookies,
|
@@ -111,7 +110,7 @@ function NextAuthPage(props) {
|
|
111
110
|
account,
|
112
111
|
profile
|
113
112
|
}) : {};
|
114
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
113
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list); // ID
|
115
114
|
|
116
115
|
const data = _objectSpread({
|
117
116
|
[identityField]: identity
|
@@ -119,11 +118,9 @@ function NextAuthPage(props) {
|
|
119
118
|
|
120
119
|
if (!result.success) {
|
121
120
|
if (!autoCreate) {
|
122
|
-
console.log('`autoCreate` is set to `false`, skipping user auto-creation');
|
123
121
|
return false;
|
124
122
|
}
|
125
123
|
|
126
|
-
console.log('`autoCreate` is set to `true`, auto-creating a new user');
|
127
124
|
const createUser = await list.createOne({
|
128
125
|
data
|
129
126
|
}).then(returned => {
|
@@ -132,14 +129,12 @@ function NextAuthPage(props) {
|
|
132
129
|
user: returned
|
133
130
|
};
|
134
131
|
}).catch(error => {
|
135
|
-
console.
|
132
|
+
console.error(error);
|
136
133
|
throw new Error(error);
|
137
134
|
});
|
138
|
-
console.log('Created User', createUser);
|
139
135
|
return createUser.success;
|
140
136
|
}
|
141
137
|
|
142
|
-
console.log('Data', data);
|
143
138
|
const updateUser = await list.updateOne({
|
144
139
|
where: {
|
145
140
|
id: result.item.id
|
@@ -151,7 +146,7 @@ function NextAuthPage(props) {
|
|
151
146
|
user: returned
|
152
147
|
};
|
153
148
|
}).catch(error => {
|
154
|
-
console.
|
149
|
+
console.error(error);
|
155
150
|
throw new Error(error);
|
156
151
|
});
|
157
152
|
return updateUser.success;
|
@@ -182,7 +177,6 @@ function NextAuthPage(props) {
|
|
182
177
|
});
|
183
178
|
}
|
184
179
|
|
185
|
-
console.log('Session', returnSession);
|
186
180
|
return returnSession;
|
187
181
|
},
|
188
182
|
|
@@ -190,7 +184,7 @@ function NextAuthPage(props) {
|
|
190
184
|
token
|
191
185
|
}) {
|
192
186
|
const identity = token.sub;
|
193
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
187
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list);
|
194
188
|
|
195
189
|
if (!result.success) {
|
196
190
|
token.itemId = null;
|
package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.prod.js
CHANGED
@@ -81,7 +81,6 @@ function NextAuthPage(props) {
|
|
81
81
|
}
|
82
82
|
|
83
83
|
const list = query[listKey];
|
84
|
-
const queryAPI = query[listKey];
|
85
84
|
const protectIdentities = true;
|
86
85
|
return NextAuth__default["default"]({
|
87
86
|
cookies,
|
@@ -111,7 +110,7 @@ function NextAuthPage(props) {
|
|
111
110
|
account,
|
112
111
|
profile
|
113
112
|
}) : {};
|
114
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
113
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list); // ID
|
115
114
|
|
116
115
|
const data = _objectSpread({
|
117
116
|
[identityField]: identity
|
@@ -119,11 +118,9 @@ function NextAuthPage(props) {
|
|
119
118
|
|
120
119
|
if (!result.success) {
|
121
120
|
if (!autoCreate) {
|
122
|
-
console.log('`autoCreate` is set to `false`, skipping user auto-creation');
|
123
121
|
return false;
|
124
122
|
}
|
125
123
|
|
126
|
-
console.log('`autoCreate` is set to `true`, auto-creating a new user');
|
127
124
|
const createUser = await list.createOne({
|
128
125
|
data
|
129
126
|
}).then(returned => {
|
@@ -132,14 +129,12 @@ function NextAuthPage(props) {
|
|
132
129
|
user: returned
|
133
130
|
};
|
134
131
|
}).catch(error => {
|
135
|
-
console.
|
132
|
+
console.error(error);
|
136
133
|
throw new Error(error);
|
137
134
|
});
|
138
|
-
console.log('Created User', createUser);
|
139
135
|
return createUser.success;
|
140
136
|
}
|
141
137
|
|
142
|
-
console.log('Data', data);
|
143
138
|
const updateUser = await list.updateOne({
|
144
139
|
where: {
|
145
140
|
id: result.item.id
|
@@ -151,7 +146,7 @@ function NextAuthPage(props) {
|
|
151
146
|
user: returned
|
152
147
|
};
|
153
148
|
}).catch(error => {
|
154
|
-
console.
|
149
|
+
console.error(error);
|
155
150
|
throw new Error(error);
|
156
151
|
});
|
157
152
|
return updateUser.success;
|
@@ -182,7 +177,6 @@ function NextAuthPage(props) {
|
|
182
177
|
});
|
183
178
|
}
|
184
179
|
|
185
|
-
console.log('Session', returnSession);
|
186
180
|
return returnSession;
|
187
181
|
},
|
188
182
|
|
@@ -190,7 +184,7 @@ function NextAuthPage(props) {
|
|
190
184
|
token
|
191
185
|
}) {
|
192
186
|
const identity = token.sub;
|
193
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
187
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list);
|
194
188
|
|
195
189
|
if (!result.success) {
|
196
190
|
token.itemId = null;
|
@@ -73,7 +73,6 @@ function NextAuthPage(props) {
|
|
73
73
|
}
|
74
74
|
|
75
75
|
const list = query[listKey];
|
76
|
-
const queryAPI = query[listKey];
|
77
76
|
const protectIdentities = true;
|
78
77
|
return NextAuth({
|
79
78
|
cookies,
|
@@ -103,7 +102,7 @@ function NextAuthPage(props) {
|
|
103
102
|
account,
|
104
103
|
profile
|
105
104
|
}) : {};
|
106
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
105
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list); // ID
|
107
106
|
|
108
107
|
const data = _objectSpread({
|
109
108
|
[identityField]: identity
|
@@ -111,11 +110,9 @@ function NextAuthPage(props) {
|
|
111
110
|
|
112
111
|
if (!result.success) {
|
113
112
|
if (!autoCreate) {
|
114
|
-
console.log('`autoCreate` is set to `false`, skipping user auto-creation');
|
115
113
|
return false;
|
116
114
|
}
|
117
115
|
|
118
|
-
console.log('`autoCreate` is set to `true`, auto-creating a new user');
|
119
116
|
const createUser = await list.createOne({
|
120
117
|
data
|
121
118
|
}).then(returned => {
|
@@ -124,14 +121,12 @@ function NextAuthPage(props) {
|
|
124
121
|
user: returned
|
125
122
|
};
|
126
123
|
}).catch(error => {
|
127
|
-
console.
|
124
|
+
console.error(error);
|
128
125
|
throw new Error(error);
|
129
126
|
});
|
130
|
-
console.log('Created User', createUser);
|
131
127
|
return createUser.success;
|
132
128
|
}
|
133
129
|
|
134
|
-
console.log('Data', data);
|
135
130
|
const updateUser = await list.updateOne({
|
136
131
|
where: {
|
137
132
|
id: result.item.id
|
@@ -143,7 +138,7 @@ function NextAuthPage(props) {
|
|
143
138
|
user: returned
|
144
139
|
};
|
145
140
|
}).catch(error => {
|
146
|
-
console.
|
141
|
+
console.error(error);
|
147
142
|
throw new Error(error);
|
148
143
|
});
|
149
144
|
return updateUser.success;
|
@@ -174,7 +169,6 @@ function NextAuthPage(props) {
|
|
174
169
|
});
|
175
170
|
}
|
176
171
|
|
177
|
-
console.log('Session', returnSession);
|
178
172
|
return returnSession;
|
179
173
|
},
|
180
174
|
|
@@ -182,7 +176,7 @@ function NextAuthPage(props) {
|
|
182
176
|
token
|
183
177
|
}) {
|
184
178
|
const identity = token.sub;
|
185
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
179
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list);
|
186
180
|
|
187
181
|
if (!result.success) {
|
188
182
|
token.itemId = null;
|
package/src/index.ts
CHANGED
@@ -194,19 +194,18 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
|
|
194
194
|
const withItemData = (
|
195
195
|
_sessionStrategy: AuthSessionStrategy<Record<string, any>>
|
196
196
|
): AuthSessionStrategy<{ listKey: string; itemId: string; data: any }> => {
|
197
|
-
const { get, ...sessionStrategy } = _sessionStrategy;
|
197
|
+
const { get, end, ...sessionStrategy } = _sessionStrategy;
|
198
198
|
return {
|
199
199
|
...sessionStrategy,
|
200
|
-
start: async () => {
|
201
|
-
return 'false';
|
202
|
-
},
|
203
200
|
get: async ({ req, createContext }) => {
|
204
|
-
const sudoContext = createContext({ sudo: true });
|
205
201
|
const pathname = url.parse(req?.url!).pathname!;
|
206
202
|
let nextSession: Session;
|
207
203
|
if (pathname.includes('/api/auth')) {
|
208
204
|
return;
|
209
205
|
}
|
206
|
+
const sudoContext = createContext({ sudo: true });
|
207
|
+
|
208
|
+
const session = await get({ req, createContext });
|
210
209
|
if (req.headers?.authorization?.split(' ')[0] === 'Bearer') {
|
211
210
|
nextSession = (await getToken({
|
212
211
|
req,
|
@@ -227,13 +226,15 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
|
|
227
226
|
return;
|
228
227
|
}
|
229
228
|
return {
|
229
|
+
...session,
|
230
230
|
...nextSession,
|
231
231
|
data: nextSession.data,
|
232
232
|
listKey: nextSession.listKey,
|
233
233
|
itemId: nextSession.itemId,
|
234
234
|
};
|
235
235
|
},
|
236
|
-
end: async ({ res, req }) => {
|
236
|
+
end: async ({ res, req, createContext }) => {
|
237
|
+
await end({ res, req, createContext });
|
237
238
|
const TOKEN_NAME =
|
238
239
|
process.env.NODE_ENV === 'production'
|
239
240
|
? '__Secure-next-auth.session-token'
|
@@ -4,28 +4,28 @@ import { Provider } from 'next-auth/providers';
|
|
4
4
|
import { JWTOptions } from 'next-auth/jwt';
|
5
5
|
import { validateNextAuth } from '../lib/validateNextAuth';
|
6
6
|
|
7
|
-
type
|
7
|
+
export type NextAuthTemplateProps = {
|
8
8
|
autoCreate: boolean;
|
9
|
-
cookies?: Partial<CookiesOptions>;
|
10
|
-
events?: Partial<EventCallbacks>;
|
11
9
|
identityField: string;
|
12
|
-
jwt?: Partial<JWTOptions>;
|
13
10
|
listKey: string;
|
14
|
-
pages?: Partial<PagesOptions>;
|
15
|
-
providers?: Provider[];
|
16
|
-
resolver?: (args: { user: any; profile: any; account: any }) => {
|
17
|
-
[key: string]: boolean | string | number;
|
18
|
-
};
|
19
11
|
sessionData: string | undefined;
|
20
12
|
sessionSecret: string;
|
21
13
|
};
|
22
14
|
|
23
|
-
type
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
export type CoreNextAuthPageProps = {
|
16
|
+
cookies?: Partial<CookiesOptions>;
|
17
|
+
events?: Partial<EventCallbacks>;
|
18
|
+
jwt?: Partial<JWTOptions>;
|
19
|
+
pages?: Partial<PagesOptions>;
|
20
|
+
providers: Provider[];
|
21
|
+
resolver?: (args: { user: any; profile: any; account: any }) => Promise<{
|
22
|
+
[key: string]: boolean | string | number;
|
23
|
+
}>;
|
24
|
+
} & NextAuthTemplateProps;
|
27
25
|
|
28
|
-
export type NextAuthPageProps = CoreNextAuthPageProps &
|
26
|
+
export type NextAuthPageProps = CoreNextAuthPageProps & {
|
27
|
+
query: KeystoneListsAPI<any>;
|
28
|
+
};
|
29
29
|
|
30
30
|
export default function NextAuthPage(props: NextAuthPageProps) {
|
31
31
|
const {
|
@@ -54,7 +54,6 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
54
54
|
}
|
55
55
|
|
56
56
|
const list = query[listKey];
|
57
|
-
const queryAPI = query[listKey];
|
58
57
|
const protectIdentities = true;
|
59
58
|
|
60
59
|
return NextAuth({
|
@@ -76,7 +75,7 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
76
75
|
}
|
77
76
|
const userInput = resolver ? await resolver({ user, account, profile }) : {};
|
78
77
|
|
79
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
78
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list);
|
80
79
|
// ID
|
81
80
|
const data: any = {
|
82
81
|
[identityField]: identity,
|
@@ -85,10 +84,8 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
85
84
|
|
86
85
|
if (!result.success) {
|
87
86
|
if (!autoCreate) {
|
88
|
-
console.log('`autoCreate` is set to `false`, skipping user auto-creation');
|
89
87
|
return false;
|
90
88
|
}
|
91
|
-
console.log('`autoCreate` is set to `true`, auto-creating a new user');
|
92
89
|
|
93
90
|
const createUser = await list
|
94
91
|
.createOne({ data })
|
@@ -96,13 +93,11 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
96
93
|
return { success: true, user: returned };
|
97
94
|
})
|
98
95
|
.catch(error => {
|
99
|
-
console.
|
96
|
+
console.error(error);
|
100
97
|
throw new Error(error);
|
101
98
|
});
|
102
|
-
console.log('Created User', createUser);
|
103
99
|
return createUser.success;
|
104
100
|
}
|
105
|
-
console.log('Data', data);
|
106
101
|
|
107
102
|
const updateUser = await list
|
108
103
|
.updateOne({ where: { id: result.item.id }, data })
|
@@ -110,7 +105,7 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
110
105
|
return { success: true, user: returned };
|
111
106
|
})
|
112
107
|
.catch(error => {
|
113
|
-
console.
|
108
|
+
console.error(error);
|
114
109
|
throw new Error(error);
|
115
110
|
});
|
116
111
|
return updateUser.success;
|
@@ -131,13 +126,12 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
131
126
|
itemId: token.itemId as string,
|
132
127
|
};
|
133
128
|
}
|
134
|
-
console.log('Session', returnSession);
|
135
129
|
|
136
130
|
return returnSession;
|
137
131
|
},
|
138
132
|
async jwt({ token }) {
|
139
133
|
const identity = token.sub as number | string;
|
140
|
-
const result = await validateNextAuth(identityField, identity, protectIdentities,
|
134
|
+
const result = await validateNextAuth(identityField, identity, protectIdentities, list);
|
141
135
|
|
142
136
|
if (!result.success) {
|
143
137
|
token.itemId = null;
|
package/src/templates/auth.ts
CHANGED
@@ -1,10 +1,15 @@
|
|
1
1
|
import ejs from 'ejs';
|
2
|
-
import {
|
2
|
+
import { NextAuthTemplateProps } from '../pages/NextAuthPage';
|
3
3
|
|
4
4
|
const template = `
|
5
5
|
import getNextAuthPage from '@opensaas/keystone-nextjs-auth/pages/NextAuthPage';
|
6
|
-
import { query } from '.keystone/api';
|
7
6
|
import keystoneConfig from '../../../../../keystone';
|
7
|
+
import { PrismaClient } from '.prisma/client';
|
8
|
+
import { createQueryAPI } from '@keystone-6/core/___internal-do-not-use-will-break-in-patch/node-api';
|
9
|
+
|
10
|
+
const keystoneQueryAPI = global.keystoneQueryAPI || createQueryAPI(keystoneConfig, PrismaClient);
|
11
|
+
|
12
|
+
if (process.env.NODE_ENV !== 'production') globalThis.keystoneQueryAPI = keystoneQueryAPI
|
8
13
|
|
9
14
|
export default getNextAuthPage({
|
10
15
|
autoCreate: <%= autoCreate %>,
|
@@ -12,22 +17,20 @@ export default getNextAuthPage({
|
|
12
17
|
listKey: '<%= listKey %>',
|
13
18
|
pages: keystoneConfig.pages,
|
14
19
|
providers: keystoneConfig.providers,
|
15
|
-
query,
|
20
|
+
query: keystoneQueryAPI,
|
16
21
|
resolver: keystoneConfig.resolver,
|
17
22
|
sessionData: '<%= sessionData %>',
|
18
23
|
sessionSecret: '<%= sessionSecret %>',
|
19
24
|
});
|
20
25
|
`;
|
21
26
|
|
22
|
-
type AuthTemplateOptions = NextAuthPageProps;
|
23
|
-
|
24
27
|
export const authTemplate = ({
|
25
28
|
autoCreate,
|
26
29
|
identityField,
|
27
30
|
listKey,
|
28
31
|
sessionData,
|
29
32
|
sessionSecret,
|
30
|
-
}:
|
33
|
+
}: NextAuthTemplateProps) => {
|
31
34
|
const authOut = ejs.render(template, {
|
32
35
|
identityField,
|
33
36
|
sessionData,
|