@opensaas/keystone-nextjs-auth 20.1.0 → 20.3.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @opensaas-keystone/nextjs-auth
2
2
 
3
+ ## 20.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 34e9932: Fix up stale next session - session is now refreshed on change of data
8
+
9
+ ## 20.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 844f069: Generate NodeAPI by def, minor cleanups
14
+
15
+ ### Patch Changes
16
+
17
+ - 6d63b1f: Minor Patch upgrades
18
+
19
+ ## 20.1.1
20
+
21
+ ### Patch Changes
22
+
23
+ - 0b22f90: Fix Error where signin was returning deny on create user
24
+
3
25
  ## 20.1.0
4
26
 
5
27
  ### Minor 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(list: any, identityField: string, identity: string | number, protectIdentities: boolean, itemAPI: KeystoneListsAPI<any>[string]): Promise<{
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 = [Provider];
16
+ export declare type NextAuthProviders = Provider[];
17
17
  declare type KeytoneAuthProviders = {
18
18
  providers: NextAuthProviders;
19
19
  };
@@ -490,6 +490,9 @@ function createAuth({
490
490
  session,
491
491
  providers,
492
492
  lists: _objectSpread({}, keystoneConfig.lists),
493
+ experimental: _objectSpread(_objectSpread({}, keystoneConfig.experimental), {}, {
494
+ generateNodeAPI: true
495
+ }),
493
496
  extendGraphqlSchema: existingExtendGraphQLSchema ? schema => existingExtendGraphQLSchema(extendGraphqlSchema(schema)) : extendGraphqlSchema
494
497
  });
495
498
  };
@@ -486,6 +486,9 @@ function createAuth({
486
486
  session,
487
487
  providers,
488
488
  lists: _objectSpread({}, keystoneConfig.lists),
489
+ experimental: _objectSpread(_objectSpread({}, keystoneConfig.experimental), {}, {
490
+ generateNodeAPI: true
491
+ }),
489
492
  extendGraphqlSchema: existingExtendGraphQLSchema ? schema => existingExtendGraphQLSchema(extendGraphqlSchema(schema)) : extendGraphqlSchema
490
493
  });
491
494
  };
@@ -457,6 +457,9 @@ function createAuth({
457
457
  session,
458
458
  providers,
459
459
  lists: _objectSpread({}, keystoneConfig.lists),
460
+ experimental: _objectSpread(_objectSpread({}, keystoneConfig.experimental), {}, {
461
+ generateNodeAPI: true
462
+ }),
460
463
  extendGraphqlSchema: existingExtendGraphQLSchema ? schema => existingExtendGraphQLSchema(extendGraphqlSchema(schema)) : extendGraphqlSchema
461
464
  });
462
465
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensaas/keystone-nextjs-auth",
3
- "version": "20.1.0",
3
+ "version": "20.3.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.4",
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.0"
17
+ "next-auth": "^4.1.2"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@keystone-6/core": "^1.0.1",
@@ -3,10 +3,12 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
6
+ var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/stringify');
6
7
  var NextAuth = require('next-auth');
7
8
 
8
9
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
9
10
 
11
+ var _JSON$stringify__default = /*#__PURE__*/_interopDefault(_JSON$stringify);
10
12
  var NextAuth__default = /*#__PURE__*/_interopDefault(NextAuth);
11
13
 
12
14
  async function findMatchingIdentity(identityField, identity, queryAPI) {
@@ -35,7 +37,7 @@ async function findMatchingIdentity(identityField, identity, queryAPI) {
35
37
  };
36
38
  }
37
39
 
38
- async function validateNextAuth(list, identityField, identity, protectIdentities, itemAPI) {
40
+ async function validateNextAuth(identityField, identity, protectIdentities, itemAPI) {
39
41
  const match = await findMatchingIdentity(identityField, identity, itemAPI);
40
42
  const {
41
43
  item
@@ -85,9 +87,11 @@ function NextAuthPage(props) {
85
87
  identity = user.id;
86
88
  } else if (typeof user.id === 'number') {
87
89
  identity = user.id;
90
+ } else {
91
+ identity = 0;
88
92
  }
89
93
 
90
- const result = await validateNextAuth(list, identityField, identity, protectIdentities, queryAPI);
94
+ const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
91
95
  const data = {}; // eslint-disable-next-line no-restricted-syntax
92
96
 
93
97
  for (const key in userMap) {
@@ -112,24 +116,26 @@ function NextAuthPage(props) {
112
116
 
113
117
  if (!result.success) {
114
118
  if (!autoCreate) {
115
- console.log('False');
119
+ console.log('`autoCreate` if set to `false`, skipping user auto-creation');
116
120
  return false;
117
121
  }
118
122
 
119
- console.log('Create User');
120
- await list.createOne({
123
+ console.log('`autoCreate` if set to `true`, auto-creating a new user');
124
+ const createUser = await list.createOne({
121
125
  data
122
126
  }).then(returned => {
123
- console.log(returned);
127
+ console.log('User Created', _JSON$stringify__default["default"](returned));
124
128
  return true;
125
129
  }).catch(error => {
126
130
  console.log(error);
127
131
  throw new Error(error);
128
132
  });
129
- } else {
130
- // await list.updateOne({where: {id: result.item.id}, data});
131
- return result.success;
132
- }
133
+ console.log('Created User', createUser);
134
+ return createUser;
135
+ } // await list.updateOne({where: {id: result.item.id}, data});
136
+
137
+
138
+ return result.success;
133
139
  },
134
140
 
135
141
  async redirect({
@@ -158,30 +164,29 @@ function NextAuthPage(props) {
158
164
  const identity = token.sub;
159
165
 
160
166
  if (!token.itemId) {
161
- const result = await validateNextAuth(list, identityField, identity, protectIdentities, queryAPI);
167
+ const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
162
168
 
163
169
  if (!result.success) {
164
- return;
170
+ return token;
165
171
  }
166
172
 
167
- const data = await query[listKey].findOne({
168
- where: {
169
- id: result.item.id
170
- },
171
- query: sessionData || 'id'
172
- });
173
+ token.itemId = result.item.id;
174
+ }
173
175
 
174
- const returnToken = _objectSpread(_objectSpread({}, token), {}, {
175
- data,
176
- subject: token.sub,
177
- listKey,
178
- itemId: result.item.id.toString()
179
- });
176
+ const data = await query[listKey].findOne({
177
+ where: {
178
+ id: token.itemId
179
+ },
180
+ query: sessionData || 'id'
181
+ });
180
182
 
181
- return returnToken;
182
- }
183
+ const returnToken = _objectSpread(_objectSpread({}, token), {}, {
184
+ data,
185
+ subject: token.sub,
186
+ listKey
187
+ });
183
188
 
184
- return token;
189
+ return returnToken;
185
190
  }
186
191
 
187
192
  }
@@ -3,10 +3,12 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
6
+ var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/stringify');
6
7
  var NextAuth = require('next-auth');
7
8
 
8
9
  function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
9
10
 
11
+ var _JSON$stringify__default = /*#__PURE__*/_interopDefault(_JSON$stringify);
10
12
  var NextAuth__default = /*#__PURE__*/_interopDefault(NextAuth);
11
13
 
12
14
  async function findMatchingIdentity(identityField, identity, queryAPI) {
@@ -35,7 +37,7 @@ async function findMatchingIdentity(identityField, identity, queryAPI) {
35
37
  };
36
38
  }
37
39
 
38
- async function validateNextAuth(list, identityField, identity, protectIdentities, itemAPI) {
40
+ async function validateNextAuth(identityField, identity, protectIdentities, itemAPI) {
39
41
  const match = await findMatchingIdentity(identityField, identity, itemAPI);
40
42
  const {
41
43
  item
@@ -85,9 +87,11 @@ function NextAuthPage(props) {
85
87
  identity = user.id;
86
88
  } else if (typeof user.id === 'number') {
87
89
  identity = user.id;
90
+ } else {
91
+ identity = 0;
88
92
  }
89
93
 
90
- const result = await validateNextAuth(list, identityField, identity, protectIdentities, queryAPI);
94
+ const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
91
95
  const data = {}; // eslint-disable-next-line no-restricted-syntax
92
96
 
93
97
  for (const key in userMap) {
@@ -112,24 +116,26 @@ function NextAuthPage(props) {
112
116
 
113
117
  if (!result.success) {
114
118
  if (!autoCreate) {
115
- console.log('False');
119
+ console.log('`autoCreate` if set to `false`, skipping user auto-creation');
116
120
  return false;
117
121
  }
118
122
 
119
- console.log('Create User');
120
- await list.createOne({
123
+ console.log('`autoCreate` if set to `true`, auto-creating a new user');
124
+ const createUser = await list.createOne({
121
125
  data
122
126
  }).then(returned => {
123
- console.log(returned);
127
+ console.log('User Created', _JSON$stringify__default["default"](returned));
124
128
  return true;
125
129
  }).catch(error => {
126
130
  console.log(error);
127
131
  throw new Error(error);
128
132
  });
129
- } else {
130
- // await list.updateOne({where: {id: result.item.id}, data});
131
- return result.success;
132
- }
133
+ console.log('Created User', createUser);
134
+ return createUser;
135
+ } // await list.updateOne({where: {id: result.item.id}, data});
136
+
137
+
138
+ return result.success;
133
139
  },
134
140
 
135
141
  async redirect({
@@ -158,30 +164,29 @@ function NextAuthPage(props) {
158
164
  const identity = token.sub;
159
165
 
160
166
  if (!token.itemId) {
161
- const result = await validateNextAuth(list, identityField, identity, protectIdentities, queryAPI);
167
+ const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
162
168
 
163
169
  if (!result.success) {
164
- return;
170
+ return token;
165
171
  }
166
172
 
167
- const data = await query[listKey].findOne({
168
- where: {
169
- id: result.item.id
170
- },
171
- query: sessionData || 'id'
172
- });
173
+ token.itemId = result.item.id;
174
+ }
173
175
 
174
- const returnToken = _objectSpread(_objectSpread({}, token), {}, {
175
- data,
176
- subject: token.sub,
177
- listKey,
178
- itemId: result.item.id.toString()
179
- });
176
+ const data = await query[listKey].findOne({
177
+ where: {
178
+ id: token.itemId
179
+ },
180
+ query: sessionData || 'id'
181
+ });
180
182
 
181
- return returnToken;
182
- }
183
+ const returnToken = _objectSpread(_objectSpread({}, token), {}, {
184
+ data,
185
+ subject: token.sub,
186
+ listKey
187
+ });
183
188
 
184
- return token;
189
+ return returnToken;
185
190
  }
186
191
 
187
192
  }
@@ -1,4 +1,5 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
2
+ import _JSON$stringify from '@babel/runtime-corejs3/core-js-stable/json/stringify';
2
3
  import NextAuth from 'next-auth';
3
4
 
4
5
  async function findMatchingIdentity(identityField, identity, queryAPI) {
@@ -27,7 +28,7 @@ async function findMatchingIdentity(identityField, identity, queryAPI) {
27
28
  };
28
29
  }
29
30
 
30
- async function validateNextAuth(list, identityField, identity, protectIdentities, itemAPI) {
31
+ async function validateNextAuth(identityField, identity, protectIdentities, itemAPI) {
31
32
  const match = await findMatchingIdentity(identityField, identity, itemAPI);
32
33
  const {
33
34
  item
@@ -77,9 +78,11 @@ function NextAuthPage(props) {
77
78
  identity = user.id;
78
79
  } else if (typeof user.id === 'number') {
79
80
  identity = user.id;
81
+ } else {
82
+ identity = 0;
80
83
  }
81
84
 
82
- const result = await validateNextAuth(list, identityField, identity, protectIdentities, queryAPI);
85
+ const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
83
86
  const data = {}; // eslint-disable-next-line no-restricted-syntax
84
87
 
85
88
  for (const key in userMap) {
@@ -104,24 +107,26 @@ function NextAuthPage(props) {
104
107
 
105
108
  if (!result.success) {
106
109
  if (!autoCreate) {
107
- console.log('False');
110
+ console.log('`autoCreate` if set to `false`, skipping user auto-creation');
108
111
  return false;
109
112
  }
110
113
 
111
- console.log('Create User');
112
- await list.createOne({
114
+ console.log('`autoCreate` if set to `true`, auto-creating a new user');
115
+ const createUser = await list.createOne({
113
116
  data
114
117
  }).then(returned => {
115
- console.log(returned);
118
+ console.log('User Created', _JSON$stringify(returned));
116
119
  return true;
117
120
  }).catch(error => {
118
121
  console.log(error);
119
122
  throw new Error(error);
120
123
  });
121
- } else {
122
- // await list.updateOne({where: {id: result.item.id}, data});
123
- return result.success;
124
- }
124
+ console.log('Created User', createUser);
125
+ return createUser;
126
+ } // await list.updateOne({where: {id: result.item.id}, data});
127
+
128
+
129
+ return result.success;
125
130
  },
126
131
 
127
132
  async redirect({
@@ -150,30 +155,29 @@ function NextAuthPage(props) {
150
155
  const identity = token.sub;
151
156
 
152
157
  if (!token.itemId) {
153
- const result = await validateNextAuth(list, identityField, identity, protectIdentities, queryAPI);
158
+ const result = await validateNextAuth(identityField, identity, protectIdentities, queryAPI);
154
159
 
155
160
  if (!result.success) {
156
- return;
161
+ return token;
157
162
  }
158
163
 
159
- const data = await query[listKey].findOne({
160
- where: {
161
- id: result.item.id
162
- },
163
- query: sessionData || 'id'
164
- });
164
+ token.itemId = result.item.id;
165
+ }
165
166
 
166
- const returnToken = _objectSpread(_objectSpread({}, token), {}, {
167
- data,
168
- subject: token.sub,
169
- listKey,
170
- itemId: result.item.id.toString()
171
- });
167
+ const data = await query[listKey].findOne({
168
+ where: {
169
+ id: token.itemId
170
+ },
171
+ query: sessionData || 'id'
172
+ });
172
173
 
173
- return returnToken;
174
- }
174
+ const returnToken = _objectSpread(_objectSpread({}, token), {}, {
175
+ data,
176
+ subject: token.sub,
177
+ listKey
178
+ });
175
179
 
176
- return token;
180
+ return returnToken;
177
181
  }
178
182
 
179
183
  }
package/src/index.ts CHANGED
@@ -292,6 +292,10 @@ export function createAuth<GeneratedListTypes extends BaseListTypeInfo>({
292
292
  lists: {
293
293
  ...keystoneConfig.lists,
294
294
  },
295
+ experimental: {
296
+ ...keystoneConfig.experimental,
297
+ generateNodeAPI: true,
298
+ },
295
299
  extendGraphqlSchema: existingExtendGraphQLSchema
296
300
  ? (schema) => existingExtendGraphQLSchema(extendGraphqlSchema(schema))
297
301
  : extendGraphqlSchema,
@@ -3,7 +3,6 @@ import { NextAuthErrorCode } from '../types';
3
3
  import { findMatchingIdentity } from './findMatchingIdentity';
4
4
 
5
5
  export async function validateNextAuth(
6
- list: any,
7
6
  identityField: string,
8
7
  identity: string | number,
9
8
  protectIdentities: boolean,
@@ -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,27 +76,30 @@ export default function NextAuthPage(props: NextAuthPageProps) {
75
76
 
76
77
  if (!result.success) {
77
78
  if (!autoCreate) {
78
- console.log('False');
79
+ console.log(
80
+ '`autoCreate` if set to `false`, skipping user auto-creation'
81
+ );
79
82
  return false;
80
83
  }
81
- console.log('Create User');
84
+ console.log(
85
+ '`autoCreate` if set to `true`, auto-creating a new user'
86
+ );
82
87
 
83
- await list
88
+ const createUser = await list
84
89
  .createOne({ data })
85
90
  .then((returned) => {
86
- console.log(returned);
87
-
91
+ console.log('User Created', JSON.stringify(returned));
88
92
  return true;
89
93
  })
90
94
  .catch((error) => {
91
95
  console.log(error);
92
-
93
96
  throw new Error(error);
94
97
  });
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
  }
101
+ // await list.updateOne({where: {id: result.item.id}, data});
102
+ return result.success;
99
103
  },
100
104
  async redirect({ url }) {
101
105
  return url;
@@ -111,10 +115,9 @@ export default function NextAuthPage(props: NextAuthPageProps) {
111
115
  return returnSession;
112
116
  },
113
117
  async jwt({ token }) {
114
- const identity = token.sub;
118
+ const identity = token.sub as number | string;
115
119
  if (!token.itemId) {
116
120
  const result = await validateNextAuth(
117
- list,
118
121
  identityField,
119
122
  identity,
120
123
  protectIdentities,
@@ -122,24 +125,22 @@ export default function NextAuthPage(props: NextAuthPageProps) {
122
125
  );
123
126
 
124
127
  if (!result.success) {
125
- return;
128
+ return token;
126
129
  }
127
-
128
- const data = await query[listKey].findOne({
129
- where: { id: result.item.id },
130
- query: sessionData || 'id',
131
- });
132
- const returnToken = {
133
- ...token,
134
- data,
135
- subject: token.sub,
136
- listKey,
137
- itemId: result.item.id.toString(),
138
- };
139
-
140
- return returnToken;
130
+ token.itemId = result.item.id;
141
131
  }
142
- return token;
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;
143
144
  },
144
145
  },
145
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 = [Provider];
15
+ export type NextAuthProviders = Provider[];
16
16
 
17
17
  type KeytoneAuthProviders = {
18
18
  providers: NextAuthProviders;