@opensaas/keystone-nextjs-auth 20.2.0 → 20.5.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +18 -0
- package/dist/opensaas-keystone-nextjs-auth.cjs.dev.js +24 -2
- package/dist/opensaas-keystone-nextjs-auth.cjs.prod.js +24 -2
- package/dist/opensaas-keystone-nextjs-auth.esm.js +24 -2
- package/package.json +7 -7
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.dev.js +17 -16
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.prod.js +17 -16
- package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.esm.js +17 -16
- package/src/index.ts +16 -3
- package/src/pages/NextAuthPage.tsx +14 -16
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,23 @@
|
|
1
1
|
# @opensaas-keystone/nextjs-auth
|
2
2
|
|
3
|
+
## 20.5.0
|
4
|
+
|
5
|
+
### Minor Changes
|
6
|
+
|
7
|
+
- e2e7122: upgrade keystone to `1.1.0`
|
8
|
+
|
9
|
+
## 20.4.0
|
10
|
+
|
11
|
+
### Minor Changes
|
12
|
+
|
13
|
+
- 80ba444: Add `getToken` to keystone `get session` to enable JWT in Authorization header
|
14
|
+
|
15
|
+
## 20.3.0
|
16
|
+
|
17
|
+
### Minor Changes
|
18
|
+
|
19
|
+
- 34e9932: Fix up stale next session - session is now refreshed on change of data
|
20
|
+
|
3
21
|
## 20.2.0
|
4
22
|
|
5
23
|
### Minor Changes
|
@@ -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
|
});
|
@@ -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
|
});
|
@@ -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
|
});
|
package/package.json
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
{
|
2
2
|
"name": "@opensaas/keystone-nextjs-auth",
|
3
|
-
"version": "20.
|
3
|
+
"version": "20.5.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",
|
7
7
|
"module": "dist/opensaas-keystone-nextjs-auth.esm.js",
|
8
8
|
"dependencies": {
|
9
|
-
"@babel/runtime": "^7.
|
10
|
-
"@babel/runtime-corejs3": "^7.
|
9
|
+
"@babel/runtime": "^7.17.8",
|
10
|
+
"@babel/runtime-corejs3": "^7.17.8",
|
11
11
|
"@types/ejs": "^3.1.0",
|
12
|
-
"cookie": "^0.4.
|
12
|
+
"cookie": "^0.4.2",
|
13
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.3.1"
|
18
18
|
},
|
19
19
|
"devDependencies": {
|
20
|
-
"@keystone-6/core": "^1.0
|
20
|
+
"@keystone-6/core": "^1.1.0",
|
21
21
|
"react": "^17.0.2"
|
22
22
|
},
|
23
23
|
"peerDependencies": {
|
24
|
-
"@keystone-6/core": "^1.0
|
24
|
+
"@keystone-6/core": "^1.1.0",
|
25
25
|
"react": "^17.0.2"
|
26
26
|
},
|
27
27
|
"engines": {
|
@@ -167,27 +167,28 @@ function NextAuthPage(props) {
|
|
167
167
|
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
168
168
|
|
169
169
|
if (!result.success) {
|
170
|
-
return
|
170
|
+
return {
|
171
|
+
result: false
|
172
|
+
};
|
171
173
|
}
|
172
174
|
|
173
|
-
|
174
|
-
|
175
|
-
id: result.item.id
|
176
|
-
},
|
177
|
-
query: sessionData || 'id'
|
178
|
-
});
|
175
|
+
token.itemId = result.item.id;
|
176
|
+
}
|
179
177
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
178
|
+
const data = await query[listKey].findOne({
|
179
|
+
where: {
|
180
|
+
id: token.itemId
|
181
|
+
},
|
182
|
+
query: sessionData || 'id'
|
183
|
+
});
|
186
184
|
|
187
|
-
|
188
|
-
|
185
|
+
const returnToken = _objectSpread(_objectSpread({}, token), {}, {
|
186
|
+
data,
|
187
|
+
subject: token.sub,
|
188
|
+
listKey
|
189
|
+
});
|
189
190
|
|
190
|
-
return
|
191
|
+
return returnToken;
|
191
192
|
}
|
192
193
|
|
193
194
|
}
|
package/pages/NextAuthPage/dist/opensaas-keystone-nextjs-auth-pages-NextAuthPage.cjs.prod.js
CHANGED
@@ -167,27 +167,28 @@ function NextAuthPage(props) {
|
|
167
167
|
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
168
168
|
|
169
169
|
if (!result.success) {
|
170
|
-
return
|
170
|
+
return {
|
171
|
+
result: false
|
172
|
+
};
|
171
173
|
}
|
172
174
|
|
173
|
-
|
174
|
-
|
175
|
-
id: result.item.id
|
176
|
-
},
|
177
|
-
query: sessionData || 'id'
|
178
|
-
});
|
175
|
+
token.itemId = result.item.id;
|
176
|
+
}
|
179
177
|
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
178
|
+
const data = await query[listKey].findOne({
|
179
|
+
where: {
|
180
|
+
id: token.itemId
|
181
|
+
},
|
182
|
+
query: sessionData || 'id'
|
183
|
+
});
|
186
184
|
|
187
|
-
|
188
|
-
|
185
|
+
const returnToken = _objectSpread(_objectSpread({}, token), {}, {
|
186
|
+
data,
|
187
|
+
subject: token.sub,
|
188
|
+
listKey
|
189
|
+
});
|
189
190
|
|
190
|
-
return
|
191
|
+
return returnToken;
|
191
192
|
}
|
192
193
|
|
193
194
|
}
|
@@ -158,27 +158,28 @@ function NextAuthPage(props) {
|
|
158
158
|
const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
|
159
159
|
|
160
160
|
if (!result.success) {
|
161
|
-
return
|
161
|
+
return {
|
162
|
+
result: false
|
163
|
+
};
|
162
164
|
}
|
163
165
|
|
164
|
-
|
165
|
-
|
166
|
-
id: result.item.id
|
167
|
-
},
|
168
|
-
query: sessionData || 'id'
|
169
|
-
});
|
166
|
+
token.itemId = result.item.id;
|
167
|
+
}
|
170
168
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
169
|
+
const data = await query[listKey].findOne({
|
170
|
+
where: {
|
171
|
+
id: token.itemId
|
172
|
+
},
|
173
|
+
query: sessionData || 'id'
|
174
|
+
});
|
177
175
|
|
178
|
-
|
179
|
-
|
176
|
+
const returnToken = _objectSpread(_objectSpread({}, token), {}, {
|
177
|
+
data,
|
178
|
+
subject: token.sub,
|
179
|
+
listKey
|
180
|
+
});
|
180
181
|
|
181
|
-
return
|
182
|
+
return returnToken;
|
182
183
|
}
|
183
184
|
|
184
185
|
}
|
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
|
},
|
@@ -125,24 +125,22 @@ export default function NextAuthPage(props: NextAuthPageProps) {
|
|
125
125
|
);
|
126
126
|
|
127
127
|
if (!result.success) {
|
128
|
-
return;
|
128
|
+
return { result: false };
|
129
129
|
}
|
130
|
-
|
131
|
-
const data = await query[listKey].findOne({
|
132
|
-
where: { id: result.item.id },
|
133
|
-
query: sessionData || 'id',
|
134
|
-
});
|
135
|
-
const returnToken = {
|
136
|
-
...token,
|
137
|
-
data,
|
138
|
-
subject: token.sub,
|
139
|
-
listKey,
|
140
|
-
itemId: result.item.id.toString(),
|
141
|
-
};
|
142
|
-
|
143
|
-
return returnToken;
|
130
|
+
token.itemId = result.item.id;
|
144
131
|
}
|
145
|
-
|
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;
|
146
144
|
},
|
147
145
|
},
|
148
146
|
});
|