@flink-app/generic-auth-plugin 0.11.6 → 0.11.8

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.
@@ -1,4 +1,4 @@
1
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
1
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
2
2
  import { autoRegisteredHandlers, HttpMethod } from "@flink-app/flink";
3
3
  import * as UserCreate_0 from "../src/handlers/UserCreate";
4
4
  import * as UserLogin_0 from "../src/handlers/UserLogin";
@@ -1,4 +1,4 @@
1
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
1
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
2
2
  import { autoRegisteredJobs } from "@flink-app/flink";
3
3
  export const jobs = [];
4
4
  autoRegisteredJobs.push(...jobs);
@@ -1,4 +1,4 @@
1
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
1
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
2
2
  import { autoRegisteredRepos } from "@flink-app/flink";
3
3
  export const repos = [];
4
4
  autoRegisteredRepos.push(...repos);
@@ -327,7 +327,7 @@
327
327
  "properties": {},
328
328
  "definitions": {}
329
329
  },
330
- "UserPushRegisterToken_7_ReqSchema": {
330
+ "UserPushRegisterToken_8_ReqSchema": {
331
331
  "$schema": "http://json-schema.org/draft-07/schema#",
332
332
  "type": "object",
333
333
  "additionalProperties": false,
@@ -353,7 +353,7 @@
353
353
  ],
354
354
  "definitions": {}
355
355
  },
356
- "UserPushRegisterToken_7_ResSchema": {
356
+ "UserPushRegisterToken_8_ResSchema": {
357
357
  "$schema": "http://json-schema.org/draft-07/schema#",
358
358
  "type": "object",
359
359
  "additionalProperties": false,
@@ -29,7 +29,7 @@ import { PutManagementUserRolesByUseridRes } from "../../src/schemas/Management/
29
29
  import { PutManagementUserUsernameByUseridReq } from "../../src/schemas/Management/PutUserUsernameByUseridReq";
30
30
  import { PutManagementUserUsernameByUseridRes } from "../../src/schemas/Management/PutUserUsernameByUseridRes";
31
31
 
32
- // Generated Thu Jan 30 2025 11:53:43 GMT+0100 (Central European Standard Time)
32
+ // Generated Wed Feb 05 2025 08:57:30 GMT+0100 (Central European Standard Time)
33
33
  export interface UserCreate_7_ReqSchema extends UserCreateReq {}
34
34
 
35
35
  export interface UserCreate_7_ResSchema extends UserCreateRes {}
@@ -60,9 +60,9 @@ export interface UserProfilePut_5_ReqSchema extends UserProfile {}
60
60
 
61
61
  export interface UserProfilePut_5_ResSchema extends UserProfile {}
62
62
 
63
- export interface UserPushRegisterToken_7_ReqSchema extends PushNotificationToken {}
63
+ export interface UserPushRegisterToken_8_ReqSchema extends PushNotificationToken {}
64
64
 
65
- export interface UserPushRegisterToken_7_ResSchema extends PushNotificatioNTokenRes {}
65
+ export interface UserPushRegisterToken_8_ResSchema extends PushNotificatioNTokenRes {}
66
66
 
67
67
  export interface UserPushRemoveToken_7_ReqSchema extends PushNotificationToken {}
68
68
 
package/.flink/start.ts CHANGED
@@ -1,4 +1,4 @@
1
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
1
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
2
2
  import "./generatedHandlers";
3
3
  import "./generatedRepos";
4
4
  import "./generatedJobs";
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.handlers = void 0;
4
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
4
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
5
5
  var flink_1 = require("@flink-app/flink");
6
6
  exports.handlers = [];
7
7
  flink_1.autoRegisteredHandlers.push.apply(flink_1.autoRegisteredHandlers, exports.handlers);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.jobs = void 0;
4
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
4
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
5
5
  var flink_1 = require("@flink-app/flink");
6
6
  exports.jobs = [];
7
7
  flink_1.autoRegisteredJobs.push.apply(flink_1.autoRegisteredJobs, exports.jobs);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.repos = void 0;
4
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
4
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
5
5
  var flink_1 = require("@flink-app/flink");
6
6
  exports.repos = [];
7
7
  flink_1.autoRegisteredRepos.push.apply(flink_1.autoRegisteredRepos, exports.repos);
@@ -58,9 +58,9 @@ export interface UserProfilePut_5_ReqSchema extends UserProfile {
58
58
  }
59
59
  export interface UserProfilePut_5_ResSchema extends UserProfile {
60
60
  }
61
- export interface UserPushRegisterToken_7_ReqSchema extends PushNotificationToken {
61
+ export interface UserPushRegisterToken_8_ReqSchema extends PushNotificationToken {
62
62
  }
63
- export interface UserPushRegisterToken_7_ResSchema extends PushNotificatioNTokenRes {
63
+ export interface UserPushRegisterToken_8_ResSchema extends PushNotificatioNTokenRes {
64
64
  }
65
65
  export interface UserPushRemoveToken_7_ReqSchema extends PushNotificationToken {
66
66
  }
@@ -327,7 +327,7 @@
327
327
  "properties": {},
328
328
  "definitions": {}
329
329
  },
330
- "UserPushRegisterToken_7_ReqSchema": {
330
+ "UserPushRegisterToken_8_ReqSchema": {
331
331
  "$schema": "http://json-schema.org/draft-07/schema#",
332
332
  "type": "object",
333
333
  "additionalProperties": false,
@@ -353,7 +353,7 @@
353
353
  ],
354
354
  "definitions": {}
355
355
  },
356
- "UserPushRegisterToken_7_ResSchema": {
356
+ "UserPushRegisterToken_8_ResSchema": {
357
357
  "$schema": "http://json-schema.org/draft-07/schema#",
358
358
  "type": "object",
359
359
  "additionalProperties": false,
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- // Generated Thu Jan 30 2025 11:53:42 GMT+0100 (Central European Standard Time)
3
+ // Generated Wed Feb 05 2025 08:57:29 GMT+0100 (Central European Standard Time)
4
4
  require("./generatedHandlers");
5
5
  require("./generatedRepos");
6
6
  require("./generatedJobs");
@@ -27,6 +27,19 @@ export interface GenericAuthPluginOptions {
27
27
  onSuccessfulLogin?: {
28
28
  (user: User): Promise<void>;
29
29
  };
30
+ /**
31
+ * If true, when a new device is registered, all other devices identified by `deviceId`
32
+ * will be deregistered to avoid duplicate notifications.
33
+ *
34
+ * Also as safety measure, any usage of the same firebase token on other users will be
35
+ * deregistered.
36
+ */
37
+ deregisterOtherDevices?: boolean;
38
+ /**
39
+ * If true, multiple devices can be registered with the same `deviceId`.
40
+ * Default is `true`.
41
+ */
42
+ allowMultipleDevices?: boolean;
30
43
  }
31
44
  export interface GenericAuthsmsOptions {
32
45
  smsClient: smsClient;
@@ -41,15 +41,19 @@ var flink_1 = require("@flink-app/flink");
41
41
  var postUserPushRegisterTokenHandler = function (_a) {
42
42
  var ctx = _a.ctx, req = _a.req, origin = _a.origin;
43
43
  return __awaiter(void 0, void 0, void 0, function () {
44
- var pluginName, repo, user, exToken;
45
- return __generator(this, function (_b) {
46
- switch (_b.label) {
44
+ var pluginName, pluginOptions, repo, deregisterOtherDevices, allowMultipleDevices, user, exToken, otherRegistrations, _i, otherRegistrations_1, other, e_1;
45
+ var _b;
46
+ return __generator(this, function (_c) {
47
+ switch (_c.label) {
47
48
  case 0:
48
49
  pluginName = origin || "genericAuthPlugin";
49
- repo = ctx.repos[ctx.plugins[pluginName].repoName];
50
+ pluginOptions = ctx.plugins[pluginName];
51
+ repo = ctx.repos[pluginOptions.repoName];
52
+ deregisterOtherDevices = pluginOptions.deregisterOtherDevices || false;
53
+ allowMultipleDevices = (_b = pluginOptions.allowMultipleDevices) !== null && _b !== void 0 ? _b : true;
50
54
  return [4 /*yield*/, repo.getById(req.user._id)];
51
55
  case 1:
52
- user = _b.sent();
56
+ user = _c.sent();
53
57
  if (user == null) {
54
58
  return [2 /*return*/, flink_1.notFound("User not found")];
55
59
  }
@@ -60,12 +64,45 @@ var postUserPushRegisterTokenHandler = function (_a) {
60
64
  else {
61
65
  user.pushNotificationTokens.push(req.body);
62
66
  }
67
+ if (!allowMultipleDevices) {
68
+ // Filter out all other devices except the newly registered one
69
+ user.pushNotificationTokens = user.pushNotificationTokens.filter(function (t) { return t.deviceId === req.body.deviceId; });
70
+ }
63
71
  return [4 /*yield*/, repo.updateOne(user._id, {
64
72
  pushNotificationTokens: user.pushNotificationTokens,
65
73
  })];
66
74
  case 2:
67
- _b.sent();
68
- return [2 /*return*/, { data: { status: "success" } }];
75
+ _c.sent();
76
+ if (!deregisterOtherDevices) return [3 /*break*/, 9];
77
+ return [4 /*yield*/, repo.findAll({
78
+ $or: [{ "pushNotificationTokens.deviceId": req.body.deviceId }, { "pushNotificationTokens.token": req.body.token }],
79
+ _id: { $ne: user._id },
80
+ })];
81
+ case 3:
82
+ otherRegistrations = _c.sent();
83
+ _i = 0, otherRegistrations_1 = otherRegistrations;
84
+ _c.label = 4;
85
+ case 4:
86
+ if (!(_i < otherRegistrations_1.length)) return [3 /*break*/, 9];
87
+ other = otherRegistrations_1[_i];
88
+ _c.label = 5;
89
+ case 5:
90
+ _c.trys.push([5, 7, , 8]);
91
+ other.pushNotificationTokens = other.pushNotificationTokens.filter(function (t) { return t.deviceId !== req.body.deviceId && t.token !== req.body.token; });
92
+ return [4 /*yield*/, repo.updateOne(other._id, {
93
+ pushNotificationTokens: other.pushNotificationTokens,
94
+ })];
95
+ case 6:
96
+ _c.sent();
97
+ return [3 /*break*/, 8];
98
+ case 7:
99
+ e_1 = _c.sent();
100
+ console.error("Error deregistering other devices", e_1);
101
+ return [3 /*break*/, 8];
102
+ case 8:
103
+ _i++;
104
+ return [3 /*break*/, 4];
105
+ case 9: return [2 /*return*/, { data: { status: "success" } }];
69
106
  }
70
107
  });
71
108
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flink-app/generic-auth-plugin",
3
- "version": "0.11.6",
3
+ "version": "0.11.8",
4
4
  "description": "Flink plugin that provides a generic user authentification solution.",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\"",
@@ -30,5 +30,5 @@
30
30
  "ts-node": "^9.1.1",
31
31
  "typescript": "^4.2.4"
32
32
  },
33
- "gitHead": "dba26157e6a41c9186a3ff87b821a1e9ea5dfa22"
33
+ "gitHead": "da8c8ba96bdae7b1abea1f4d3d8f8969f2da3894"
34
34
  }
@@ -22,8 +22,22 @@ export interface GenericAuthPluginOptions {
22
22
  usernameFormat?: RegExp;
23
23
  sms?: GenericAuthsmsOptions;
24
24
  onSuccessfulLogin?: {
25
- (user:User): Promise<void>
25
+ (user: User): Promise<void>;
26
26
  };
27
+ /**
28
+ * If true, when a new device is registered, all other devices identified by `deviceId`
29
+ * will be deregistered to avoid duplicate notifications.
30
+ *
31
+ * Also as safety measure, any usage of the same firebase token on other users will be
32
+ * deregistered.
33
+ */
34
+ deregisterOtherDevices?: boolean;
35
+
36
+ /**
37
+ * If true, multiple devices can be registered with the same `deviceId`.
38
+ * Default is `true`.
39
+ */
40
+ allowMultipleDevices?: boolean;
27
41
  }
28
42
 
29
43
  export interface GenericAuthsmsOptions {
@@ -3,14 +3,18 @@ import { genericAuthContext } from "../genericAuthContext";
3
3
  import { PushNotificationToken } from "../schemas/PushNotificationToken";
4
4
  import { PushNotificatioNTokenRes } from "../schemas/PushNotificationTokenRes";
5
5
  import { User } from "../schemas/User";
6
+ import { GenericAuthPluginOptions } from "../genericAuthPluginOptions";
6
7
 
7
8
  const postUserPushRegisterTokenHandler: Handler<FlinkContext<genericAuthContext>, PushNotificationToken, PushNotificatioNTokenRes> = async ({
8
9
  ctx,
9
10
  req,
10
11
  origin,
11
12
  }) => {
12
- let pluginName = origin || "genericAuthPlugin";
13
- let repo = ctx.repos[(<any>ctx.plugins)[pluginName].repoName];
13
+ const pluginName = origin || "genericAuthPlugin";
14
+ const pluginOptions: GenericAuthPluginOptions = (ctx.plugins as any)[pluginName];
15
+ const repo = ctx.repos[pluginOptions.repoName];
16
+ const deregisterOtherDevices = pluginOptions.deregisterOtherDevices || false;
17
+ const allowMultipleDevices = pluginOptions.allowMultipleDevices ?? true;
14
18
 
15
19
  const user = <User>await repo.getById(req.user._id);
16
20
 
@@ -26,10 +30,33 @@ const postUserPushRegisterTokenHandler: Handler<FlinkContext<genericAuthContext>
26
30
  user.pushNotificationTokens.push(req.body);
27
31
  }
28
32
 
33
+ if (!allowMultipleDevices) {
34
+ // Filter out all other devices except the newly registered one
35
+ user.pushNotificationTokens = user.pushNotificationTokens.filter((t) => t.deviceId === req.body.deviceId);
36
+ }
37
+
29
38
  await repo.updateOne(user._id, {
30
39
  pushNotificationTokens: user.pushNotificationTokens,
31
40
  });
32
41
 
42
+ if (deregisterOtherDevices) {
43
+ const otherRegistrations = <User[]>await repo.findAll({
44
+ $or: [{ "pushNotificationTokens.deviceId": req.body.deviceId }, { "pushNotificationTokens.token": req.body.token }],
45
+ _id: { $ne: user._id },
46
+ });
47
+
48
+ for (let other of otherRegistrations) {
49
+ try {
50
+ other.pushNotificationTokens = other.pushNotificationTokens.filter((t) => t.deviceId !== req.body.deviceId && t.token !== req.body.token);
51
+ await repo.updateOne(other._id, {
52
+ pushNotificationTokens: other.pushNotificationTokens,
53
+ });
54
+ } catch (e) {
55
+ console.error("Error deregistering other devices", e);
56
+ }
57
+ }
58
+ }
59
+
33
60
  return { data: { status: "success" } };
34
61
  };
35
62