@opensaas/keystone-nextjs-auth 20.3.0 → 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 CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 20.3.0
4
10
 
5
11
  ### 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 sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
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 sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
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 sessionStrategy = _objectWithoutProperties(_sessionStrategy, _excluded);
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensaas/keystone-nextjs-auth",
3
- "version": "20.3.0",
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",
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
  },