@flink-app/generic-auth-plugin 0.12.1-alpha.0 → 0.12.1-alpha.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.
Files changed (38) hide show
  1. package/.flink/generatedHandlers.ts +1 -1
  2. package/.flink/generatedJobs.ts +1 -1
  3. package/.flink/generatedRepos.ts +1 -1
  4. package/.flink/schemas/schemas.json +2 -4
  5. package/.flink/schemas/schemas.ts +1 -1
  6. package/.flink/start.ts +1 -1
  7. package/dist/.flink/generatedHandlers.js +1 -1
  8. package/dist/.flink/generatedJobs.js +1 -1
  9. package/dist/.flink/generatedRepos.js +1 -1
  10. package/dist/.flink/schemas/schemas.json +2 -4
  11. package/dist/.flink/start.js +1 -1
  12. package/dist/src/coreFunctions.js +8 -8
  13. package/dist/src/handlers/Management/DeleteUserByUserid.js +21 -23
  14. package/dist/src/handlers/Management/GetSchema.js +12 -14
  15. package/dist/src/handlers/Management/GetUser.js +21 -23
  16. package/dist/src/handlers/Management/GetUserByUserid.js +22 -24
  17. package/dist/src/handlers/Management/GetUserViewByUserid.js +34 -36
  18. package/dist/src/handlers/Management/PutUserPasswordByUserid.js +46 -48
  19. package/dist/src/handlers/Management/PutUserProfileByUserid.js +24 -26
  20. package/dist/src/handlers/Management/PutUserProfileByUseridAppend.js +27 -29
  21. package/dist/src/handlers/Management/PutUserRolesByUserid.js +24 -26
  22. package/dist/src/handlers/Management/PutUserUsernameByUserid.js +33 -35
  23. package/dist/src/handlers/UserCreate.js +44 -46
  24. package/dist/src/handlers/UserLogin.js +23 -25
  25. package/dist/src/handlers/UserLoginByToken.js +23 -25
  26. package/dist/src/handlers/UserPasswordPut.js +21 -23
  27. package/dist/src/handlers/UserPasswordResetComplete.js +27 -29
  28. package/dist/src/handlers/UserPasswordResetForm.js +11 -11
  29. package/dist/src/handlers/UserPasswordResetStart.js +53 -55
  30. package/dist/src/handlers/UserProfileGet.js +21 -23
  31. package/dist/src/handlers/UserProfilePut.js +28 -30
  32. package/dist/src/handlers/UserPushRegisterToken.js +72 -74
  33. package/dist/src/handlers/UserPushRemoveToken.js +25 -27
  34. package/dist/src/handlers/UserToken.js +24 -26
  35. package/dist/src/index.js +7 -8
  36. package/dist/src/init.js +8 -4
  37. package/dist/src/management.js +5 -1
  38. package/package.json +32 -32
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
18
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -38,35 +38,33 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.__schemas = exports.__params = exports.__query = exports.__file = exports.__assumedHttpMethod = void 0;
40
40
  var flink_1 = require("@flink-app/flink");
41
- var postPasswordResetCompleteHandler = function (_a) {
42
- var ctx = _a.ctx, req = _a.req, origin = _a.origin;
43
- return __awaiter(void 0, void 0, void 0, function () {
44
- var pluginName, repo, jwtSecret /*, numberOfDigits, lifeTime*/, resp;
45
- return __generator(this, function (_b) {
46
- switch (_b.label) {
47
- case 0:
48
- pluginName = origin || "genericAuthPlugin";
49
- repo = ctx.repos[ctx.plugins[pluginName].repoName];
50
- if (ctx.plugins.genericAuthPlugin.passwordResetSettings == null) {
51
- return [2 /*return*/, flink_1.internalServerError("Password reset settings is needed to use password-reset")];
52
- }
53
- jwtSecret = ctx.plugins[pluginName].passwordResetSettings.code.jwtSecret;
54
- return [4 /*yield*/, ctx.plugins.genericAuthPlugin.passwordResetComplete(repo, ctx.auth, jwtSecret, req.body.passwordResetToken, req.body.code, req.body.password, ctx.plugins.genericAuthPlugin.createPasswordHashAndSaltMethod, ctx.plugins.genericAuthPlugin.passwordResetSettings.passwordResetReusableTokens)];
55
- case 1:
56
- resp = _b.sent();
57
- switch (resp.status) {
58
- case "invalidCode":
59
- return [2 /*return*/, flink_1.badRequest("Invalid validation code", resp.status)];
60
- case "passwordError":
61
- return [2 /*return*/, flink_1.badRequest("Invalid password", resp.status)];
62
- case "userNotFound":
63
- return [2 /*return*/, flink_1.notFound("User not found", resp.status)];
64
- }
65
- return [2 /*return*/, { data: resp, status: 200 }];
66
- }
67
- });
41
+ var postPasswordResetCompleteHandler = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
42
+ var pluginName, repo, jwtSecret /*, numberOfDigits, lifeTime*/, resp;
43
+ var ctx = _b.ctx, req = _b.req, origin = _b.origin;
44
+ return __generator(this, function (_c) {
45
+ switch (_c.label) {
46
+ case 0:
47
+ pluginName = origin || "genericAuthPlugin";
48
+ repo = ctx.repos[ctx.plugins[pluginName].repoName];
49
+ if (ctx.plugins.genericAuthPlugin.passwordResetSettings == null) {
50
+ return [2 /*return*/, (0, flink_1.internalServerError)("Password reset settings is needed to use password-reset")];
51
+ }
52
+ jwtSecret = ctx.plugins[pluginName].passwordResetSettings.code.jwtSecret;
53
+ return [4 /*yield*/, ctx.plugins.genericAuthPlugin.passwordResetComplete(repo, ctx.auth, jwtSecret, req.body.passwordResetToken, req.body.code, req.body.password, ctx.plugins.genericAuthPlugin.createPasswordHashAndSaltMethod, ctx.plugins.genericAuthPlugin.passwordResetSettings.passwordResetReusableTokens)];
54
+ case 1:
55
+ resp = _c.sent();
56
+ switch (resp.status) {
57
+ case "invalidCode":
58
+ return [2 /*return*/, (0, flink_1.badRequest)("Invalid validation code", resp.status)];
59
+ case "passwordError":
60
+ return [2 /*return*/, (0, flink_1.badRequest)("Invalid password", resp.status)];
61
+ case "userNotFound":
62
+ return [2 /*return*/, (0, flink_1.notFound)("User not found", resp.status)];
63
+ }
64
+ return [2 /*return*/, { data: resp, status: 200 }];
65
+ }
68
66
  });
69
- };
67
+ }); };
70
68
  exports.default = postPasswordResetCompleteHandler;
71
69
  exports.__assumedHttpMethod = "", exports.__file = "UserPasswordResetComplete.ts", exports.__query = [], exports.__params = [];
72
70
  exports.__schemas = { reqSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "passwordResetToken": { "type": "string" }, "code": { "type": "string" }, "password": { "type": "string" } }, "required": ["code", "password", "passwordResetToken"], "definitions": {} }, resSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "status": { "type": "string", "enum": ["success", "userNotFound", "invalidCode", "passwordError"] } }, "required": ["status"], "definitions": {} } };
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
18
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -46,15 +46,15 @@ var handlebars_1 = __importDefault(require("handlebars"));
46
46
  var defaultTemplate = "<html>\n<head>\n <title>Password reset</title>\n <link rel=\"stylesheet\" href=\"form/style.css\" />\n <script src=\"form/script.js\" type=\"text/javascript\"></script>\n</head>\n<body>\n <form id=\"form\">\n <p>Please enter new password</p>\n <input type=\"password\" name=\"password\" placeholder=\"Enter new password\" />\n <input\n type=\"password\"\n name=\"confirmPassword\"\n placeholder=\"Confirm new password\"\n />\n <button id=\"submit-btn\">Submit</button>\n </form>\n <div id=\"success\">Password has been updated, please proceed to login.</div>\n</body>\n</html>\n";
47
47
  var script = "\n window.onload = () => {\n const urlSearchParams = new URLSearchParams(window.location.search);\n const params = Object.fromEntries(urlSearchParams.entries());\n const { token, code } = params;\n\n if (!token) {\n alert(\"Missing token\");\n } else if (!code) {\n alert(\"Missing code\");\n }\n\n const submitBtnEl = document.getElementById(\"submit-btn\");\n const [passwordInputEl, confirmPasswordEl] =\n document.getElementsByTagName(\"input\");\n\n submitBtnEl.onclick = async (e) => {\n e.preventDefault();\n e.stopPropagation();\n\n if (!passwordInputEl.value) {\n return alert(\"Enter a new password\");\n } else if (passwordInputEl.value !== confirmPasswordEl.value) {\n return alert(\n \"Passwords does not match, make sure that new and confirmed passwords are the same\"\n );\n }\n\n const res = await window.fetch(\"{{completeUrl}}\", {\n method: \"POST\",\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n passwordResetToken: token,\n code: code,\n password: passwordInputEl.value,\n }),\n });\n\n if (res.status > 399) {\n alert(\"Failed to set new password\");\n } else {\n document.getElementById(\"form\").style.display = \"none\";\n document.getElementById(\"success\").style.display = \"block\";\n }\n };\n };\n";
48
48
  var style = " * {\n box-sizing: border-box;\n font-family: Arial, Helvetica, sans-serif;\n }\n\n p {\n margin: 0.5rem 0;\n }\n\n body {\n padding: 1rem;\n }\n\n form {\n display: block;\n max-width: 320px;\n }\n\n input {\n width: 100%;\n display: block;\n margin: 0.5rem 0;\n }\n\n #success {\n display: none;\n font-size: 1.2rem;\n color: rgb(0, 177, 115);\n max-width: 350px;\n }\n";
49
- function handleUserPasswordResetForm(_req, res, _a) {
50
- var templateFile = _a.templateFile, completeUrl = _a.completeUrl;
51
- return __awaiter(this, void 0, void 0, function () {
49
+ function handleUserPasswordResetForm(_req_1, res_1, _a) {
50
+ return __awaiter(this, arguments, void 0, function (_req, res, _b) {
52
51
  var tpl, html;
53
- return __generator(this, function (_b) {
54
- switch (_b.label) {
52
+ var templateFile = _b.templateFile, completeUrl = _b.completeUrl;
53
+ return __generator(this, function (_c) {
54
+ switch (_c.label) {
55
55
  case 0: return [4 /*yield*/, readTemplate(templateFile)];
56
56
  case 1:
57
- tpl = _b.sent();
57
+ tpl = _c.sent();
58
58
  html = handlebars_1.default.compile(tpl)({
59
59
  completeUrl: completeUrl,
60
60
  });
@@ -65,11 +65,11 @@ function handleUserPasswordResetForm(_req, res, _a) {
65
65
  });
66
66
  }
67
67
  exports.handleUserPasswordResetForm = handleUserPasswordResetForm;
68
- function resetPasswordFormScript(req, res, _a) {
69
- var completeUrl = _a.completeUrl;
70
- return __awaiter(this, void 0, void 0, function () {
68
+ function resetPasswordFormScript(req_1, res_1, _a) {
69
+ return __awaiter(this, arguments, void 0, function (req, res, _b) {
71
70
  var js;
72
- return __generator(this, function (_b) {
71
+ var completeUrl = _b.completeUrl;
72
+ return __generator(this, function (_c) {
73
73
  res.header("Content-Type", "application/javascript");
74
74
  js = handlebars_1.default.compile(script)({ completeUrl: completeUrl });
75
75
  res.send(js);
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
18
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -42,61 +42,59 @@ Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.__schemas = exports.__params = exports.__query = exports.__file = exports.__assumedHttpMethod = void 0;
43
43
  var flink_1 = require("@flink-app/flink");
44
44
  var handlebars_1 = __importDefault(require("handlebars"));
45
- var postPasswordResetStartHandler = function (_a) {
46
- var ctx = _a.ctx, req = _a.req, origin = _a.origin;
47
- return __awaiter(void 0, void 0, void 0, function () {
48
- var pluginName, genericAuthPlugin, repo, _b, jwtSecret, numberOfDigits, lifeTime, resp, emailCtx, emailSettings, subject, html, email;
49
- var _c;
50
- return __generator(this, function (_d) {
51
- switch (_d.label) {
52
- case 0:
53
- pluginName = origin || "genericAuthPlugin";
54
- genericAuthPlugin = ctx.plugins[pluginName];
55
- repo = ctx.repos[genericAuthPlugin.repoName];
56
- if (((_c = ctx.plugins.emailPlugin) === null || _c === void 0 ? void 0 : _c.client) == null) {
57
- return [2 /*return*/, flink_1.internalServerError("Email plugin have to be initialized to use /password/reset")];
58
- }
59
- if (!genericAuthPlugin.passwordResetSettings) {
60
- return [2 /*return*/, flink_1.internalServerError("Password reset settings is needed to use /password/reset")];
61
- }
62
- _b = genericAuthPlugin.passwordResetSettings.code, jwtSecret = _b.jwtSecret, numberOfDigits = _b.numberOfDigits, lifeTime = _b.lifeTime;
63
- return [4 /*yield*/, genericAuthPlugin.passwordResetStart(repo, ctx.auth, jwtSecret, req.body.username, numberOfDigits, lifeTime, genericAuthPlugin.passwordResetSettings.passwordResetReusableTokens)];
64
- case 1:
65
- resp = _d.sent();
66
- if (resp.status != "success") {
67
- return [2 /*return*/, { data: { status: "success", passwordResetToken: resp.passwordResetToken } }];
68
- }
69
- emailCtx = {
70
- code: resp.code,
71
- passwordResetToken: resp.passwordResetToken,
72
- username: req.body.username,
73
- profile: resp.profile,
74
- template: req.body.template,
75
- additionalAuth: req.body.additionalAuth,
76
- resetPasswordFormBaseUrl: genericAuthPlugin.passwordResetSettings.resetPasswordFormBaseUrl
77
- };
78
- emailSettings = genericAuthPlugin.passwordResetSettings.email;
79
- if (typeof emailSettings == "function") {
80
- emailSettings = emailSettings(emailCtx);
81
- }
82
- subject = handlebars_1.default.compile(emailSettings.subject)(emailCtx);
83
- html = handlebars_1.default.compile(emailSettings.html)(emailCtx);
84
- email = req.body.username;
85
- return [4 /*yield*/, ctx.plugins.emailPlugin.client.send({
86
- from: emailSettings.from_address,
87
- to: [email],
88
- subject: subject,
89
- html: html,
90
- })];
91
- case 2:
92
- _d.sent();
93
- return [2 /*return*/, {
94
- data: { status: "success", passwordResetToken: resp.passwordResetToken },
95
- }];
96
- }
97
- });
45
+ var postPasswordResetStartHandler = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
46
+ var pluginName, genericAuthPlugin, repo, _c, jwtSecret, numberOfDigits, lifeTime, resp, emailCtx, emailSettings, subject, html, email;
47
+ var _d;
48
+ var ctx = _b.ctx, req = _b.req, origin = _b.origin;
49
+ return __generator(this, function (_e) {
50
+ switch (_e.label) {
51
+ case 0:
52
+ pluginName = origin || "genericAuthPlugin";
53
+ genericAuthPlugin = ctx.plugins[pluginName];
54
+ repo = ctx.repos[genericAuthPlugin.repoName];
55
+ if (((_d = ctx.plugins.emailPlugin) === null || _d === void 0 ? void 0 : _d.client) == null) {
56
+ return [2 /*return*/, (0, flink_1.internalServerError)("Email plugin have to be initialized to use /password/reset")];
57
+ }
58
+ if (!genericAuthPlugin.passwordResetSettings) {
59
+ return [2 /*return*/, (0, flink_1.internalServerError)("Password reset settings is needed to use /password/reset")];
60
+ }
61
+ _c = genericAuthPlugin.passwordResetSettings.code, jwtSecret = _c.jwtSecret, numberOfDigits = _c.numberOfDigits, lifeTime = _c.lifeTime;
62
+ return [4 /*yield*/, genericAuthPlugin.passwordResetStart(repo, ctx.auth, jwtSecret, req.body.username, numberOfDigits, lifeTime, genericAuthPlugin.passwordResetSettings.passwordResetReusableTokens)];
63
+ case 1:
64
+ resp = _e.sent();
65
+ if (resp.status != "success") {
66
+ return [2 /*return*/, { data: { status: "success", passwordResetToken: resp.passwordResetToken } }];
67
+ }
68
+ emailCtx = {
69
+ code: resp.code,
70
+ passwordResetToken: resp.passwordResetToken,
71
+ username: req.body.username,
72
+ profile: resp.profile,
73
+ template: req.body.template,
74
+ additionalAuth: req.body.additionalAuth,
75
+ resetPasswordFormBaseUrl: genericAuthPlugin.passwordResetSettings.resetPasswordFormBaseUrl
76
+ };
77
+ emailSettings = genericAuthPlugin.passwordResetSettings.email;
78
+ if (typeof emailSettings == "function") {
79
+ emailSettings = emailSettings(emailCtx);
80
+ }
81
+ subject = handlebars_1.default.compile(emailSettings.subject)(emailCtx);
82
+ html = handlebars_1.default.compile(emailSettings.html)(emailCtx);
83
+ email = req.body.username;
84
+ return [4 /*yield*/, ctx.plugins.emailPlugin.client.send({
85
+ from: emailSettings.from_address,
86
+ to: [email],
87
+ subject: subject,
88
+ html: html,
89
+ })];
90
+ case 2:
91
+ _e.sent();
92
+ return [2 /*return*/, {
93
+ data: { status: "success", passwordResetToken: resp.passwordResetToken },
94
+ }];
95
+ }
98
96
  });
99
- };
97
+ }); };
100
98
  exports.default = postPasswordResetStartHandler;
101
99
  exports.__assumedHttpMethod = "", exports.__file = "UserPasswordResetStart.ts", exports.__query = [], exports.__params = [];
102
100
  exports.__schemas = { reqSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "username": { "type": "string" }, "template": { "type": "string" }, "additionalAuth": { "type": "string" } }, "required": ["username"], "definitions": {} }, resSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "status": { "type": "string", "enum": ["success", "userNotFound"] }, "passwordResetToken": { "type": "string" }, "profile": { "type": "object" } }, "required": ["status"], "definitions": {} } };
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
14
14
  function verb(n) { return function (v) { return step([n, v]); }; }
15
15
  function step(op) {
16
16
  if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
18
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
19
  if (y = 0, t) op = [op[0] & 2, t.value];
20
20
  switch (op[0]) {
@@ -38,29 +38,27 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.__schemas = exports.__params = exports.__query = exports.__file = exports.__assumedHttpMethod = void 0;
40
40
  var flink_1 = require("@flink-app/flink");
41
- var getProfileHandler = function (_a) {
42
- var ctx = _a.ctx, req = _a.req, origin = _a.origin;
43
- return __awaiter(void 0, void 0, void 0, function () {
44
- var pluginName, repo, userId, user;
45
- return __generator(this, function (_b) {
46
- switch (_b.label) {
47
- case 0:
48
- pluginName = origin || "genericAuthPlugin";
49
- repo = ctx.repos[ctx.plugins[pluginName].repoName];
50
- userId = req.user._id;
51
- return [4 /*yield*/, repo.getById(userId)];
52
- case 1:
53
- user = _b.sent();
54
- if (user == null) {
55
- return [2 /*return*/, flink_1.notFound()];
56
- }
57
- return [2 /*return*/, {
58
- data: user.profile,
59
- }];
60
- }
61
- });
41
+ var getProfileHandler = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
42
+ var pluginName, repo, userId, user;
43
+ var ctx = _b.ctx, req = _b.req, origin = _b.origin;
44
+ return __generator(this, function (_c) {
45
+ switch (_c.label) {
46
+ case 0:
47
+ pluginName = origin || "genericAuthPlugin";
48
+ repo = ctx.repos[ctx.plugins[pluginName].repoName];
49
+ userId = req.user._id;
50
+ return [4 /*yield*/, repo.getById(userId)];
51
+ case 1:
52
+ user = _c.sent();
53
+ if (user == null) {
54
+ return [2 /*return*/, (0, flink_1.notFound)()];
55
+ }
56
+ return [2 /*return*/, {
57
+ data: user.profile,
58
+ }];
59
+ }
62
60
  });
63
- };
61
+ }); };
64
62
  exports.default = getProfileHandler;
65
63
  exports.__assumedHttpMethod = "", exports.__file = "UserProfileGet.ts", exports.__query = [], exports.__params = [];
66
64
  exports.__schemas = { reqSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": {}, "definitions": {} }, resSchema: undefined };
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
25
25
  function verb(n) { return function (v) { return step([n, v]); }; }
26
26
  function step(op) {
27
27
  if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
29
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
30
  if (y = 0, t) op = [op[0] & 2, t.value];
31
31
  switch (op[0]) {
@@ -49,36 +49,34 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.__schemas = exports.__params = exports.__query = exports.__file = exports.__assumedHttpMethod = void 0;
51
51
  var flink_1 = require("@flink-app/flink");
52
- var putUserProfileHandler = function (_a) {
53
- var ctx = _a.ctx, req = _a.req, origin = _a.origin;
54
- return __awaiter(void 0, void 0, void 0, function () {
55
- var pluginName, repo, userId, user, updatedProfile;
56
- return __generator(this, function (_b) {
57
- switch (_b.label) {
58
- case 0:
59
- pluginName = origin || "genericAuthPlugin";
60
- repo = ctx.repos[ctx.plugins[pluginName].repoName];
61
- userId = req.user._id;
62
- return [4 /*yield*/, repo.getById(userId)];
63
- case 1:
64
- user = _b.sent();
65
- if (user == null) {
66
- return [2 /*return*/, flink_1.notFound()];
67
- }
68
- updatedProfile = __assign(__assign({}, user.profile), req.body);
69
- return [4 /*yield*/, repo.updateOne(userId, { profile: updatedProfile })];
70
- case 2:
71
- _b.sent();
72
- return [4 /*yield*/, repo.getById(userId)];
73
- case 3:
74
- user = _b.sent();
75
- return [2 /*return*/, {
76
- data: user.profile,
77
- }];
78
- }
79
- });
52
+ var putUserProfileHandler = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
53
+ var pluginName, repo, userId, user, updatedProfile;
54
+ var ctx = _b.ctx, req = _b.req, origin = _b.origin;
55
+ return __generator(this, function (_c) {
56
+ switch (_c.label) {
57
+ case 0:
58
+ pluginName = origin || "genericAuthPlugin";
59
+ repo = ctx.repos[ctx.plugins[pluginName].repoName];
60
+ userId = req.user._id;
61
+ return [4 /*yield*/, repo.getById(userId)];
62
+ case 1:
63
+ user = _c.sent();
64
+ if (user == null) {
65
+ return [2 /*return*/, (0, flink_1.notFound)()];
66
+ }
67
+ updatedProfile = __assign(__assign({}, user.profile), req.body);
68
+ return [4 /*yield*/, repo.updateOne(userId, { profile: updatedProfile })];
69
+ case 2:
70
+ _c.sent();
71
+ return [4 /*yield*/, repo.getById(userId)];
72
+ case 3:
73
+ user = _c.sent();
74
+ return [2 /*return*/, {
75
+ data: user.profile,
76
+ }];
77
+ }
80
78
  });
81
- };
79
+ }); };
82
80
  exports.default = putUserProfileHandler;
83
81
  exports.__assumedHttpMethod = "", exports.__file = "UserProfilePut.ts", exports.__query = [], exports.__params = [];
84
82
  exports.__schemas = { reqSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": {}, "definitions": {} }, resSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": {}, "definitions": {} } };
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
25
25
  function verb(n) { return function (v) { return step([n, v]); }; }
26
26
  function step(op) {
27
27
  if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
29
  if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
30
  if (y = 0, t) op = [op[0] & 2, t.value];
31
31
  switch (op[0]) {
@@ -49,79 +49,77 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
49
49
  Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.__schemas = exports.__params = exports.__query = exports.__file = exports.__assumedHttpMethod = void 0;
51
51
  var flink_1 = require("@flink-app/flink");
52
- var postUserPushRegisterTokenHandler = function (_a) {
53
- var ctx = _a.ctx, req = _a.req, origin = _a.origin;
54
- return __awaiter(void 0, void 0, void 0, function () {
55
- var pluginName, pluginOptions, repo, deregisterOtherDevices, allowMultipleDevices, user, exToken, otherRegistrations, _i, otherRegistrations_1, other, lengthBefore, e_1;
56
- var _b;
57
- return __generator(this, function (_c) {
58
- switch (_c.label) {
59
- case 0:
60
- pluginName = origin || "genericAuthPlugin";
61
- pluginOptions = ctx.plugins[pluginName];
62
- repo = ctx.repos[pluginOptions.repoName];
63
- deregisterOtherDevices = pluginOptions.deregisterOtherDevices || false;
64
- allowMultipleDevices = (_b = pluginOptions.allowMultipleDevices) !== null && _b !== void 0 ? _b : true;
65
- return [4 /*yield*/, repo.getById(req.user._id)];
66
- case 1:
67
- user = _c.sent();
68
- if (user == null) {
69
- return [2 /*return*/, flink_1.notFound("User not found")];
70
- }
71
- exToken = user.pushNotificationTokens.find(function (t) { return t.deviceId == req.body.deviceId; });
72
- if (exToken != null) {
73
- exToken.token = req.body.token;
74
- exToken.type = req.body.type || "firebase";
75
- }
76
- else {
77
- user.pushNotificationTokens.push(__assign(__assign({}, req.body), { type: req.body.type || "firebase" }));
78
- }
79
- if (!allowMultipleDevices) {
80
- // Filter out all other devices except the newly registered one
81
- user.pushNotificationTokens = user.pushNotificationTokens.filter(function (t) { return t.deviceId === req.body.deviceId; });
82
- }
83
- return [4 /*yield*/, repo.updateOne(user._id, {
84
- pushNotificationTokens: user.pushNotificationTokens,
85
- })];
86
- case 2:
87
- _c.sent();
88
- if (!deregisterOtherDevices) return [3 /*break*/, 9];
89
- return [4 /*yield*/, repo.findAll({
90
- $or: [{ "pushNotificationTokens.deviceId": req.body.deviceId }, { "pushNotificationTokens.token": req.body.token }],
91
- _id: { $ne: user._id },
92
- })];
93
- case 3:
94
- otherRegistrations = _c.sent();
95
- flink_1.log.debug("Found " + otherRegistrations.length + " other registrations for device " + req.body.deviceId + " or token " + req.body.token);
96
- _i = 0, otherRegistrations_1 = otherRegistrations;
97
- _c.label = 4;
98
- case 4:
99
- if (!(_i < otherRegistrations_1.length)) return [3 /*break*/, 9];
100
- other = otherRegistrations_1[_i];
101
- _c.label = 5;
102
- case 5:
103
- _c.trys.push([5, 7, , 8]);
104
- lengthBefore = other.pushNotificationTokens.length;
105
- other.pushNotificationTokens = other.pushNotificationTokens.filter(function (t) { return t.deviceId !== req.body.deviceId && t.token !== req.body.token; });
106
- flink_1.log.debug("Deregistering " + (lengthBefore - other.pushNotificationTokens.length) + " devices for user " + other._id + " as other user " + user._id + " claimed this device");
107
- return [4 /*yield*/, repo.updateOne(other._id, {
108
- pushNotificationTokens: other.pushNotificationTokens,
109
- })];
110
- case 6:
111
- _c.sent();
112
- return [3 /*break*/, 8];
113
- case 7:
114
- e_1 = _c.sent();
115
- console.error("Error deregistering other devices", e_1);
116
- return [3 /*break*/, 8];
117
- case 8:
118
- _i++;
119
- return [3 /*break*/, 4];
120
- case 9: return [2 /*return*/, { data: { status: "success" } }];
121
- }
122
- });
52
+ var postUserPushRegisterTokenHandler = function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
53
+ var pluginName, pluginOptions, repo, deregisterOtherDevices, allowMultipleDevices, user, exToken, otherRegistrations, _i, otherRegistrations_1, other, lengthBefore, e_1;
54
+ var _c;
55
+ var ctx = _b.ctx, req = _b.req, origin = _b.origin;
56
+ return __generator(this, function (_d) {
57
+ switch (_d.label) {
58
+ case 0:
59
+ pluginName = origin || "genericAuthPlugin";
60
+ pluginOptions = ctx.plugins[pluginName];
61
+ repo = ctx.repos[pluginOptions.repoName];
62
+ deregisterOtherDevices = pluginOptions.deregisterOtherDevices || false;
63
+ allowMultipleDevices = (_c = pluginOptions.allowMultipleDevices) !== null && _c !== void 0 ? _c : true;
64
+ return [4 /*yield*/, repo.getById(req.user._id)];
65
+ case 1:
66
+ user = _d.sent();
67
+ if (user == null) {
68
+ return [2 /*return*/, (0, flink_1.notFound)("User not found")];
69
+ }
70
+ exToken = user.pushNotificationTokens.find(function (t) { return t.deviceId == req.body.deviceId; });
71
+ if (exToken != null) {
72
+ exToken.token = req.body.token;
73
+ exToken.type = req.body.type || "firebase";
74
+ }
75
+ else {
76
+ user.pushNotificationTokens.push(__assign(__assign({}, req.body), { type: req.body.type || "firebase" }));
77
+ }
78
+ if (!allowMultipleDevices) {
79
+ // Filter out all other devices except the newly registered one
80
+ user.pushNotificationTokens = user.pushNotificationTokens.filter(function (t) { return t.deviceId === req.body.deviceId; });
81
+ }
82
+ return [4 /*yield*/, repo.updateOne(user._id, {
83
+ pushNotificationTokens: user.pushNotificationTokens,
84
+ })];
85
+ case 2:
86
+ _d.sent();
87
+ if (!deregisterOtherDevices) return [3 /*break*/, 9];
88
+ return [4 /*yield*/, repo.findAll({
89
+ $or: [{ "pushNotificationTokens.deviceId": req.body.deviceId }, { "pushNotificationTokens.token": req.body.token }],
90
+ _id: { $ne: user._id },
91
+ })];
92
+ case 3:
93
+ otherRegistrations = _d.sent();
94
+ flink_1.log.debug("Found ".concat(otherRegistrations.length, " other registrations for device ").concat(req.body.deviceId, " or token ").concat(req.body.token));
95
+ _i = 0, otherRegistrations_1 = otherRegistrations;
96
+ _d.label = 4;
97
+ case 4:
98
+ if (!(_i < otherRegistrations_1.length)) return [3 /*break*/, 9];
99
+ other = otherRegistrations_1[_i];
100
+ _d.label = 5;
101
+ case 5:
102
+ _d.trys.push([5, 7, , 8]);
103
+ lengthBefore = other.pushNotificationTokens.length;
104
+ other.pushNotificationTokens = other.pushNotificationTokens.filter(function (t) { return t.deviceId !== req.body.deviceId && t.token !== req.body.token; });
105
+ flink_1.log.debug("Deregistering ".concat(lengthBefore - other.pushNotificationTokens.length, " devices for user ").concat(other._id, " as other user ").concat(user._id, " claimed this device"));
106
+ return [4 /*yield*/, repo.updateOne(other._id, {
107
+ pushNotificationTokens: other.pushNotificationTokens,
108
+ })];
109
+ case 6:
110
+ _d.sent();
111
+ return [3 /*break*/, 8];
112
+ case 7:
113
+ e_1 = _d.sent();
114
+ console.error("Error deregistering other devices", e_1);
115
+ return [3 /*break*/, 8];
116
+ case 8:
117
+ _i++;
118
+ return [3 /*break*/, 4];
119
+ case 9: return [2 /*return*/, { data: { status: "success" } }];
120
+ }
123
121
  });
124
- };
122
+ }); };
125
123
  exports.default = postUserPushRegisterTokenHandler;
126
124
  exports.__assumedHttpMethod = "", exports.__file = "UserPushRegisterToken.ts", exports.__query = [], exports.__params = [];
127
- exports.__schemas = { reqSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "deviceId": { "type": "string" }, "token": { "type": "string" }, "platform": { "type": "string", "enum": ["ios", "android", "web"] }, "type": { "type": "string", "enum": ["firebase", "apn-voip"], "description": "The type of the token. This is used to determine which push notification service to use. Default is \"firebase\"" } }, "required": ["deviceId", "token"], "definitions": {} }, resSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "status": { "type": "string", "enum": ["success", "failed"] } }, "required": ["status"], "definitions": {} } };
125
+ exports.__schemas = { reqSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "deviceId": { "type": "string" }, "token": { "type": "string" }, "platform": { "type": "string", "enum": ["ios", "android", "web"] }, "type": { "type": "string", "enum": ["firebase", "apn-voip"] } }, "required": ["deviceId", "token"], "definitions": {} }, resSchema: { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "additionalProperties": false, "properties": { "status": { "type": "string", "enum": ["success", "failed"] } }, "required": ["status"], "definitions": {} } };