@dereekb/firebase-server 10.1.30 → 11.0.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.
Files changed (125) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/mailgun/package.json +1 -1
  3. package/mailgun/src/lib/auth.mailgun.js +23 -17
  4. package/mailgun/src/lib/auth.mailgun.js.map +1 -1
  5. package/package.json +7 -1
  6. package/src/lib/auth/auth.service.js +196 -225
  7. package/src/lib/auth/auth.service.js.map +1 -1
  8. package/src/lib/auth/auth.util.js +10 -13
  9. package/src/lib/auth/auth.util.js.map +1 -1
  10. package/src/lib/firestore/driver.accessor.batch.js +8 -4
  11. package/src/lib/firestore/driver.accessor.batch.js.map +1 -1
  12. package/src/lib/firestore/driver.accessor.default.js +3 -2
  13. package/src/lib/firestore/driver.accessor.default.js.map +1 -1
  14. package/src/lib/firestore/driver.accessor.js +4 -5
  15. package/src/lib/firestore/driver.accessor.js.map +1 -1
  16. package/src/lib/firestore/driver.accessor.transaction.js +7 -3
  17. package/src/lib/firestore/driver.accessor.transaction.js.map +1 -1
  18. package/src/lib/firestore/driver.query.js +5 -2
  19. package/src/lib/firestore/driver.query.js.map +1 -1
  20. package/src/lib/firestore/increment.js +1 -1
  21. package/src/lib/firestore/increment.js.map +1 -1
  22. package/src/lib/function/assert.js +18 -25
  23. package/src/lib/function/assert.js.map +1 -1
  24. package/src/lib/function/context.js +1 -2
  25. package/src/lib/function/context.js.map +1 -1
  26. package/src/lib/function/error.auth.js +1 -1
  27. package/src/lib/function/error.auth.js.map +1 -1
  28. package/src/lib/function/error.js +60 -10
  29. package/src/lib/function/error.js.map +1 -1
  30. package/src/lib/nest/app.js +7 -8
  31. package/src/lib/nest/app.js.map +1 -1
  32. package/src/lib/nest/auth/auth.module.js +4 -2
  33. package/src/lib/nest/auth/auth.module.js.map +1 -1
  34. package/src/lib/nest/auth/auth.util.js +1 -2
  35. package/src/lib/nest/auth/auth.util.js.map +1 -1
  36. package/src/lib/nest/development/development.app.function.js +5 -4
  37. package/src/lib/nest/development/development.app.function.js.map +1 -1
  38. package/src/lib/nest/development/development.function.js +5 -1
  39. package/src/lib/nest/development/development.function.js.map +1 -1
  40. package/src/lib/nest/development/development.schedule.function.js +3 -4
  41. package/src/lib/nest/development/development.schedule.function.js.map +1 -1
  42. package/src/lib/nest/firestore/firestore.module.js +3 -4
  43. package/src/lib/nest/firestore/firestore.module.js.map +1 -1
  44. package/src/lib/nest/function/context.js +3 -1
  45. package/src/lib/nest/function/context.js.map +1 -1
  46. package/src/lib/nest/function/nest.js +8 -2
  47. package/src/lib/nest/function/nest.js.map +1 -1
  48. package/src/lib/nest/function/v1/call.js +5 -2
  49. package/src/lib/nest/function/v1/call.js.map +1 -1
  50. package/src/lib/nest/function/v1/event.js +10 -3
  51. package/src/lib/nest/function/v1/event.js.map +1 -1
  52. package/src/lib/nest/function/v2/blocking.js +4 -1
  53. package/src/lib/nest/function/v2/blocking.js.map +1 -1
  54. package/src/lib/nest/function/v2/call.js +4 -1
  55. package/src/lib/nest/function/v2/call.js.map +1 -1
  56. package/src/lib/nest/function/v2/event.js +4 -1
  57. package/src/lib/nest/function/v2/event.js.map +1 -1
  58. package/src/lib/nest/function/v2/taskqueue.js +5 -2
  59. package/src/lib/nest/function/v2/taskqueue.js.map +1 -1
  60. package/src/lib/nest/middleware/appcheck.decorator.js +2 -3
  61. package/src/lib/nest/middleware/appcheck.decorator.js.map +1 -1
  62. package/src/lib/nest/middleware/appcheck.middleware.js +24 -30
  63. package/src/lib/nest/middleware/appcheck.middleware.js.map +1 -1
  64. package/src/lib/nest/middleware/appcheck.module.js +1 -3
  65. package/src/lib/nest/middleware/appcheck.module.js.map +1 -1
  66. package/src/lib/nest/middleware/webhook.js +1 -3
  67. package/src/lib/nest/middleware/webhook.js.map +1 -1
  68. package/src/lib/nest/model/call.model.function.js +1 -2
  69. package/src/lib/nest/model/call.model.function.js.map +1 -1
  70. package/src/lib/nest/model/create.model.function.js +6 -3
  71. package/src/lib/nest/model/create.model.function.js.map +1 -1
  72. package/src/lib/nest/model/delete.model.function.js +6 -3
  73. package/src/lib/nest/model/delete.model.function.js.map +1 -1
  74. package/src/lib/nest/model/permission.error.js +4 -6
  75. package/src/lib/nest/model/permission.error.js.map +1 -1
  76. package/src/lib/nest/model/read.model.function.js +6 -3
  77. package/src/lib/nest/model/read.model.function.js.map +1 -1
  78. package/src/lib/nest/model/specifier.function.js +3 -4
  79. package/src/lib/nest/model/specifier.function.js.map +1 -1
  80. package/src/lib/nest/model/update.model.function.js +6 -3
  81. package/src/lib/nest/model/update.model.function.js.map +1 -1
  82. package/src/lib/nest/nest.provider.js +25 -31
  83. package/src/lib/nest/nest.provider.js.map +1 -1
  84. package/src/lib/nest/storage/storage.module.js +4 -2
  85. package/src/lib/nest/storage/storage.module.js.map +1 -1
  86. package/src/lib/storage/driver.accessor.js +32 -26
  87. package/src/lib/storage/driver.accessor.js.map +1 -1
  88. package/src/lib/storage/storage.service.js +1 -0
  89. package/src/lib/storage/storage.service.js.map +1 -1
  90. package/test/package.json +1 -1
  91. package/test/src/lib/firebase/firebase.admin.auth.js +55 -49
  92. package/test/src/lib/firebase/firebase.admin.auth.js.map +1 -1
  93. package/test/src/lib/firebase/firebase.admin.collection.js +9 -7
  94. package/test/src/lib/firebase/firebase.admin.collection.js.map +1 -1
  95. package/test/src/lib/firebase/firebase.admin.function.js +11 -9
  96. package/test/src/lib/firebase/firebase.admin.function.js.map +1 -1
  97. package/test/src/lib/firebase/firebase.admin.js +19 -16
  98. package/test/src/lib/firebase/firebase.admin.js.map +1 -1
  99. package/test/src/lib/firebase/firebase.admin.nest.function.js +5 -1
  100. package/test/src/lib/firebase/firebase.admin.nest.function.js.map +1 -1
  101. package/test/src/lib/firebase/firebase.admin.nest.js +11 -12
  102. package/test/src/lib/firebase/firebase.admin.nest.js.map +1 -1
  103. package/test/src/lib/firebase/firebase.admin.test.server.js +5 -6
  104. package/test/src/lib/firebase/firebase.admin.test.server.js.map +1 -1
  105. package/test/src/lib/firebase/firebase.function.js +4 -1
  106. package/test/src/lib/firebase/firebase.function.js.map +1 -1
  107. package/test/src/lib/firebase/firebase.js +1 -2
  108. package/test/src/lib/firebase/firebase.js.map +1 -1
  109. package/test/src/lib/firestore/firestore.js +7 -9
  110. package/test/src/lib/firestore/firestore.js.map +1 -1
  111. package/test/src/lib/storage/storage.js +6 -8
  112. package/test/src/lib/storage/storage.js.map +1 -1
  113. package/zoho/package.json +6 -0
  114. package/zoho/src/index.d.ts +1 -0
  115. package/zoho/src/index.js +5 -0
  116. package/zoho/src/index.js.map +1 -0
  117. package/zoho/src/lib/index.d.ts +2 -0
  118. package/zoho/src/lib/index.js +6 -0
  119. package/zoho/src/lib/index.js.map +1 -0
  120. package/zoho/src/lib/zoho.accounts.firebase.d.ts +8 -0
  121. package/zoho/src/lib/zoho.accounts.firebase.js +62 -0
  122. package/zoho/src/lib/zoho.accounts.firebase.js.map +1 -0
  123. package/zoho/src/lib/zoho.accounts.firebase.system.d.ts +32 -0
  124. package/zoho/src/lib/zoho.accounts.firebase.system.js +44 -0
  125. package/zoho/src/lib/zoho.accounts.firebase.system.js.map +1 -0
package/CHANGELOG.md CHANGED
@@ -2,6 +2,24 @@
2
2
 
3
3
  This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
4
4
 
5
+ # [11.0.0](https://github.com/dereekb/dbx-components/compare/v10.2.0-dev...v11.0.0) (2024-11-12)
6
+
7
+
8
+ ### Code Refactoring
9
+
10
+ * revisited some todos ([4902b4b](https://github.com/dereekb/dbx-components/commit/4902b4bcffde7174c37b72d84fd4473e3b975769))
11
+
12
+
13
+ ### BREAKING CHANGES
14
+
15
+ * all breaking changes are documented in VERSION_MIGRATION.md
16
+
17
+
18
+
19
+ # [10.2.0](https://github.com/dereekb/dbx-components/compare/v10.1.30-dev...v10.2.0) (2024-11-07)
20
+
21
+
22
+
5
23
  ## [10.1.30](https://github.com/dereekb/dbx-components/compare/v10.1.29-dev...v10.1.30) (2024-10-23)
6
24
 
7
25
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dereekb/firebase-server/mailgun",
3
- "version": "10.1.30",
3
+ "version": "11.0.0",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js"
6
6
  }
@@ -1,32 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractMailgunContentFirebaseServerNewUserService = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const firebase_server_1 = require("@dereekb/firebase-server");
6
5
  /**
7
6
  * Abstract FirebaseServerNewUserService implementation that sends an email to a template on Mailgun.
8
7
  */
9
8
  class AbstractMailgunContentFirebaseServerNewUserService extends firebase_server_1.AbstractFirebaseServerNewUserService {
9
+ mailgunService;
10
10
  constructor(authService, mailgunService) {
11
11
  super(authService);
12
12
  this.mailgunService = mailgunService;
13
13
  }
14
- sendSetupContentToUser(user) {
15
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
16
- const userRecord = yield user.userContext.loadRecord();
17
- const { setupPassword } = user.claims;
18
- const { uid, displayName, email } = userRecord;
19
- if (!email) {
20
- throw new Error(`Email is not present/available for the user record "${userRecord.uid}"`);
21
- }
22
- const baseRequest = yield this.buildNewUserMailgunContentRequest(user);
23
- const baseRequestTo = baseRequest.to;
24
- yield this.mailgunService.sendTemplateEmail(Object.assign(Object.assign({}, baseRequest), { to: {
25
- name: baseRequestTo === null || baseRequestTo === void 0 ? void 0 : baseRequestTo.name,
26
- email,
27
- userVariables: Object.assign(Object.assign({ setupPassword }, baseRequestTo === null || baseRequestTo === void 0 ? void 0 : baseRequestTo.userVariables), { displayName,
28
- uid })
29
- }, sendTestEmails: baseRequest.sendTestEmails || user.sendDetailsInTestEnvironment || undefined }));
14
+ async sendSetupContentToUser(user) {
15
+ const userRecord = await user.userContext.loadRecord();
16
+ const { setupPassword } = user.claims;
17
+ const { uid, displayName, email } = userRecord;
18
+ if (!email) {
19
+ throw new Error(`Email is not present/available for the user record "${userRecord.uid}"`);
20
+ }
21
+ const baseRequest = await this.buildNewUserMailgunContentRequest(user);
22
+ const baseRequestTo = baseRequest.to;
23
+ await this.mailgunService.sendTemplateEmail({
24
+ ...baseRequest,
25
+ to: {
26
+ name: baseRequestTo?.name,
27
+ email,
28
+ userVariables: {
29
+ setupPassword,
30
+ ...baseRequestTo?.userVariables,
31
+ displayName,
32
+ uid
33
+ }
34
+ },
35
+ sendTestEmails: baseRequest.sendTestEmails || user.sendDetailsInTestEnvironment || undefined
30
36
  });
31
37
  }
32
38
  }
@@ -1 +1 @@
1
- {"version":3,"file":"auth.mailgun.js","sourceRoot":"","sources":["../../../../../../packages/firebase-server/mailgun/src/lib/auth.mailgun.ts"],"names":[],"mappings":";;;;AACA,8DAAgO;AAWhO;;GAEG;AACH,MAAsB,kDAA0N,SAAQ,sDAA6C;IACnS,YAAY,WAA4C,EAAW,cAA8B;QAC/F,KAAK,CAAC,WAAW,CAAC,CAAC;QAD8C,mBAAc,GAAd,cAAc,CAAgB;IAEjG,CAAC;IAEe,sBAAsB,CAAC,IAAiD;;YACtF,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;YACvD,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;YACtC,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,GAAG,UAAU,CAAC;YAE/C,IAAI,CAAC,KAAK,EAAE;gBACV,MAAM,IAAI,KAAK,CAAC,uDAAuD,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC;aAC3F;YAED,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,iCAAiC,CAAC,IAAI,CAAC,CAAC;YACvE,MAAM,aAAa,GAAG,WAAW,CAAC,EAAE,CAAC;YAErC,MAAM,IAAI,CAAC,cAAc,CAAC,iBAAiB,iCACtC,WAAW,KACd,EAAE,EAAE;oBACF,IAAI,EAAE,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,IAAI;oBACzB,KAAK;oBACL,aAAa,gCACX,aAAa,IACV,aAAa,aAAb,aAAa,uBAAb,aAAa,CAAE,aAAa,KAC/B,WAAW;wBACX,GAAG,GACJ;iBACF,EACD,cAAc,EAAE,WAAW,CAAC,cAAc,IAAI,IAAI,CAAC,4BAA4B,IAAI,SAAS,IAC5F,CAAC;QACL,CAAC;KAAA;CAGF;AAlCD,gHAkCC"}
1
+ {"version":3,"file":"auth.mailgun.js","sourceRoot":"","sources":["../../../../../../packages/firebase-server/mailgun/src/lib/auth.mailgun.ts"],"names":[],"mappings":";;;AACA,8DAAgO;AAWhO;;GAEG;AACH,MAAsB,kDAA0N,SAAQ,sDAA6C;IAChO;IAAnE,YAAY,WAA4C,EAAW,cAA8B;QAC/F,KAAK,CAAC,WAAW,CAAC,CAAC;QAD8C,mBAAc,GAAd,cAAc,CAAgB;IAEjG,CAAC;IAES,KAAK,CAAC,sBAAsB,CAAC,IAAiD;QACtF,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,UAAU,EAAE,CAAC;QACvD,MAAM,EAAE,aAAa,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC;QACtC,MAAM,EAAE,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,GAAG,UAAU,CAAC;QAE/C,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,uDAAuD,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC;SAC3F;QAED,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,iCAAiC,CAAC,IAAI,CAAC,CAAC;QACvE,MAAM,aAAa,GAAG,WAAW,CAAC,EAAE,CAAC;QAErC,MAAM,IAAI,CAAC,cAAc,CAAC,iBAAiB,CAAC;YAC1C,GAAG,WAAW;YACd,EAAE,EAAE;gBACF,IAAI,EAAE,aAAa,EAAE,IAAI;gBACzB,KAAK;gBACL,aAAa,EAAE;oBACb,aAAa;oBACb,GAAG,aAAa,EAAE,aAAa;oBAC/B,WAAW;oBACX,GAAG;iBACJ;aACF;YACD,cAAc,EAAE,WAAW,CAAC,cAAc,IAAI,IAAI,CAAC,4BAA4B,IAAI,SAAS;SAC7F,CAAC,CAAC;IACL,CAAC;CAGF;AAlCD,gHAkCC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dereekb/firebase-server",
3
- "version": "10.1.30",
3
+ "version": "11.0.0",
4
4
  "exports": {
5
5
  ".": {
6
6
  "types": "./src/index.d.ts",
@@ -19,6 +19,12 @@
19
19
  "main": "./mailgun/src/index.js",
20
20
  "require": "./mailgun/src/index.js",
21
21
  "default": "./mailgun/src/index.js"
22
+ },
23
+ "./zoho": {
24
+ "types": "./zoho/src/index.d.ts",
25
+ "main": "./zoho/src/index.js",
26
+ "require": "./zoho/src/index.js",
27
+ "default": "./zoho/src/index.js"
22
28
  }
23
29
  },
24
30
  "main": "./src/index.js",
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractFirebaseServerAuthService = exports.FirebaseServerAuthService = exports.NoSetupContentFirebaseServerNewUserService = exports.AbstractFirebaseServerNewUserService = exports.userContextFromUid = exports.DEFAULT_SETUP_COM_THROTTLE_TIME = exports.AbstractFirebaseServerAuthContext = exports.AbstractFirebaseServerAuthUserContext = exports.DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const firebase_1 = require("@dereekb/firebase");
6
5
  const util_1 = require("@dereekb/util");
7
6
  const context_1 = require("../function/context");
@@ -10,15 +9,15 @@ const date_1 = require("@dereekb/date");
10
9
  const auth_util_1 = require("./auth.util");
11
10
  exports.DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR = (0, util_1.randomNumberFactory)({ min: 100000, max: 1000000, round: 'floor' }); // 6 digits
12
11
  class AbstractFirebaseServerAuthUserContext {
12
+ service;
13
+ uid;
14
+ _loadRecord = (0, util_1.cachedGetter)(() => this.service.auth.getUser(this.uid));
13
15
  constructor(service, uid) {
14
16
  this.service = service;
15
17
  this.uid = uid;
16
- this._loadRecord = (0, util_1.cachedGetter)(() => this.service.auth.getUser(this.uid));
17
18
  }
18
- exists() {
19
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
20
- return (0, auth_util_1.getAuthUserOrUndefined)(this._loadRecord()).then((x) => Boolean(x));
21
- });
19
+ async exists() {
20
+ return (0, auth_util_1.getAuthUserOrUndefined)(this._loadRecord()).then((x) => Boolean(x));
22
21
  }
23
22
  loadRecord() {
24
23
  return this._loadRecord();
@@ -29,74 +28,60 @@ class AbstractFirebaseServerAuthUserContext {
29
28
  _generateResetPasswordKey() {
30
29
  return String((0, exports.DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR)());
31
30
  }
32
- beginResetPassword() {
33
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
34
- const password = this._generateResetPasswordKey();
35
- const passwordClaimsData = {
36
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_PASSWORD_KEY]: password,
37
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_LAST_COM_DATE_KEY]: (0, date_1.toISODateString)(new Date())
38
- };
39
- // set the claims
40
- yield this.updateClaims(passwordClaimsData);
41
- // update the user
42
- yield this.updateUser({ password });
43
- return passwordClaimsData;
44
- });
45
- }
46
- loadResetPasswordClaims() {
47
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
48
- const claims = yield this.loadClaims();
49
- if (claims.resetPassword != null) {
50
- return claims;
51
- }
52
- else {
53
- return undefined;
54
- }
55
- });
31
+ async beginResetPassword() {
32
+ const password = this._generateResetPasswordKey();
33
+ const passwordClaimsData = {
34
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_PASSWORD_KEY]: password,
35
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_LAST_COM_DATE_KEY]: (0, date_1.toISODateString)(new Date())
36
+ };
37
+ // set the claims
38
+ await this.updateClaims(passwordClaimsData);
39
+ // update the user
40
+ await this.updateUser({ password });
41
+ return passwordClaimsData;
42
+ }
43
+ async loadResetPasswordClaims() {
44
+ const claims = await this.loadClaims();
45
+ if (claims.resetPassword != null) {
46
+ return claims;
47
+ }
48
+ else {
49
+ return undefined;
50
+ }
56
51
  }
57
52
  /**
58
53
  * Sets the user's password.
59
54
  */
60
- setPassword(password) {
61
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
62
- const record = yield this.updateUser({ password });
63
- // clear password reset claims
64
- yield this.updateClaims({
65
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_PASSWORD_KEY]: null,
66
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_LAST_COM_DATE_KEY]: null
67
- });
68
- return record;
69
- });
70
- }
71
- updateUser(template) {
72
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
73
- return this.service.auth.updateUser(this.uid, template);
74
- });
75
- }
76
- loadRoles() {
77
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
78
- const claims = yield this.loadClaims();
79
- return this.service.readRoles(claims);
80
- });
81
- }
82
- addRoles(roles) {
83
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
84
- const claims = this._claimsForRolesChange(roles);
85
- return this.updateClaims(claims);
55
+ async setPassword(password) {
56
+ const record = await this.updateUser({ password });
57
+ // clear password reset claims
58
+ await this.updateClaims({
59
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_PASSWORD_KEY]: null,
60
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_RESET_LAST_COM_DATE_KEY]: null
86
61
  });
87
- }
88
- removeRoles(roles) {
89
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
90
- const baseClaims = this._claimsForRolesChange(roles);
91
- const claims = {};
92
- (0, util_1.forEachKeyValue)(baseClaims, {
93
- forEach: ([key]) => {
94
- claims[key] = null; // set null on every key
95
- },
96
- filter: util_1.KeyValueTypleValueFilter.NONE // don't skip any key/value
97
- });
98
- return this.updateClaims(claims);
62
+ return record;
63
+ }
64
+ async updateUser(template) {
65
+ return this.service.auth.updateUser(this.uid, template);
66
+ }
67
+ async loadRoles() {
68
+ const claims = await this.loadClaims();
69
+ return this.service.readRoles(claims);
70
+ }
71
+ async addRoles(roles) {
72
+ const claims = this._claimsForRolesChange(roles);
73
+ return this.updateClaims(claims);
74
+ }
75
+ async removeRoles(roles) {
76
+ const baseClaims = this._claimsForRolesChange(roles);
77
+ const claims = {};
78
+ (0, util_1.forEachKeyValue)(baseClaims, {
79
+ forEach: ([key]) => {
80
+ claims[key] = null; // set null on every key
81
+ },
82
+ filter: util_1.KeyValueTypleValueFilter.NONE // don't skip any key/value
99
83
  });
84
+ return this.updateClaims(claims);
100
85
  }
101
86
  /**
102
87
  * Sets the claims using the input roles and roles set.
@@ -109,32 +94,34 @@ class AbstractFirebaseServerAuthUserContext {
109
94
  * @param claimsToRetain
110
95
  * @returns
111
96
  */
112
- setRoles(roles, claimsToRetain) {
113
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
114
- const claims = Object.assign(Object.assign({}, claimsToRetain), this._claimsForRolesChange(Array.from(roles)));
115
- return this.setClaims(claims);
116
- });
97
+ async setRoles(roles, claimsToRetain) {
98
+ const claims = {
99
+ ...claimsToRetain,
100
+ ...this._claimsForRolesChange(Array.from(roles))
101
+ };
102
+ return this.setClaims(claims);
117
103
  }
118
104
  _claimsForRolesChange(roles) {
119
105
  // filter null/undefined since the claims will contain null values for claims that are not related.
120
106
  return (0, util_1.filterNullAndUndefinedValues)(this.service.claimsForRoles((0, util_1.asSet)(roles)));
121
107
  }
122
108
  loadClaims() {
123
- return this.loadRecord().then((x) => { var _a; return ((_a = x.customClaims) !== null && _a !== void 0 ? _a : {}); });
124
- }
125
- updateClaims(claims) {
126
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
127
- const currentClaims = yield this.loadClaims();
128
- let newClaims;
129
- if (currentClaims) {
130
- newClaims = Object.assign(Object.assign({}, currentClaims), (0, util_1.filterUndefinedValues)(claims, false));
131
- newClaims = (0, util_1.filterNullAndUndefinedValues)(newClaims);
132
- }
133
- else {
134
- newClaims = claims;
135
- }
136
- return this.setClaims(newClaims);
137
- });
109
+ return this.loadRecord().then((x) => (x.customClaims ?? {}));
110
+ }
111
+ async updateClaims(claims) {
112
+ const currentClaims = await this.loadClaims();
113
+ let newClaims;
114
+ if (currentClaims) {
115
+ newClaims = {
116
+ ...currentClaims,
117
+ ...(0, util_1.filterUndefinedValues)(claims, false)
118
+ };
119
+ newClaims = (0, util_1.filterNullAndUndefinedValues)(newClaims);
120
+ }
121
+ else {
122
+ newClaims = claims;
123
+ }
124
+ return this.setClaims(newClaims);
138
125
  }
139
126
  clearClaims() {
140
127
  return this.setClaims(null);
@@ -147,13 +134,15 @@ class AbstractFirebaseServerAuthUserContext {
147
134
  }
148
135
  exports.AbstractFirebaseServerAuthUserContext = AbstractFirebaseServerAuthUserContext;
149
136
  class AbstractFirebaseServerAuthContext {
137
+ service;
138
+ context;
139
+ _authRoles = (0, util_1.cachedGetter)(() => this.service.readRoles(this.claims));
140
+ _isAdmin = (0, util_1.cachedGetter)(() => this.service.isAdminInRoles(this._authRoles()));
141
+ _hasSignedTos = (0, util_1.cachedGetter)(() => this.service.hasSignedTosInRoles(this._authRoles()));
142
+ _userContext = (0, util_1.cachedGetter)(() => this.service.userContext(this.context.auth.uid));
150
143
  constructor(service, context) {
151
144
  this.service = service;
152
145
  this.context = context;
153
- this._authRoles = (0, util_1.cachedGetter)(() => this.service.readRoles(this.claims));
154
- this._isAdmin = (0, util_1.cachedGetter)(() => this.service.isAdminInRoles(this._authRoles()));
155
- this._hasSignedTos = (0, util_1.cachedGetter)(() => this.service.hasSignedTosInRoles(this._authRoles()));
156
- this._userContext = (0, util_1.cachedGetter)(() => this.service.userContext(this.context.auth.uid));
157
146
  }
158
147
  get userContext() {
159
148
  return this._userContext();
@@ -189,112 +178,101 @@ function userContextFromUid(authService, userContextOrUid) {
189
178
  }
190
179
  exports.userContextFromUid = userContextFromUid;
191
180
  class AbstractFirebaseServerNewUserService {
181
+ authService;
182
+ setupThrottleTime = exports.DEFAULT_SETUP_COM_THROTTLE_TIME;
192
183
  constructor(authService) {
193
184
  this.authService = authService;
194
- this.setupThrottleTime = exports.DEFAULT_SETUP_COM_THROTTLE_TIME;
195
- }
196
- initializeNewUser(input) {
197
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
198
- const { uid, email, phone, sendSetupContent, sendSetupContentIfUserExists, data, sendDetailsInTestEnvironment } = input;
199
- let userRecordPromise;
200
- if (uid) {
201
- userRecordPromise = this.authService.auth.getUser(uid);
202
- }
203
- else if (email) {
204
- userRecordPromise = this.authService.auth.getUserByEmail(email);
205
- }
206
- else if (phone) {
207
- userRecordPromise = this.authService.auth.getUserByPhoneNumber(phone);
208
- }
209
- else {
210
- throw new Error('email or phone is required to initialize a new user.');
211
- }
212
- let userRecord = yield (0, auth_util_1.getAuthUserOrUndefined)(userRecordPromise);
213
- let userRecordId;
214
- let createdUser = false;
215
- if (!userRecord) {
216
- const createResult = yield this.createNewUser(input);
217
- // add the setup password to the user's credentials
218
- const userContext = this.authService.userContext(createResult.user.uid);
219
- yield this.addNewUserSetupClaims(userContext, createResult.password);
220
- createdUser = true;
221
- userRecordId = userContext.uid;
222
- userRecord = yield userContext.loadRecord();
223
- }
224
- else {
225
- userRecordId = userRecord.uid;
226
- }
227
- // send content if necessary
228
- if ((createdUser && sendSetupContent === true) || sendSetupContentIfUserExists) {
229
- const sentEmail = yield this.sendSetupContent(userRecordId, { data, sendDetailsInTestEnvironment });
230
- // reload the user record
231
- if (sentEmail) {
232
- const userContext = this.authService.userContext(userRecordId);
233
- userRecord = yield userContext.loadRecord();
234
- }
235
- }
236
- return userRecord;
237
- });
238
185
  }
239
- addNewUserSetupClaims(userContextOrUid, setupPassword) {
240
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
241
- const password = setupPassword !== null && setupPassword !== void 0 ? setupPassword : this.generateRandomSetupPassword();
242
- const userContext = userContextFromUid(this.authService, userContextOrUid);
243
- yield userContext.updateClaims({
244
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_SETUP_PASSWORD_KEY]: password
245
- });
246
- return userContext;
247
- });
248
- }
249
- sendSetupContent(userContextOrUid, config) {
250
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
251
- const setupDetails = yield this.loadSetupDetails(userContextOrUid, config);
252
- let sentContent = false;
253
- if (setupDetails) {
254
- const { setupCommunicationAt } = setupDetails.claims;
255
- if (!setupCommunicationAt || (0, date_1.timeHasExpired)(new Date(setupCommunicationAt), this.setupThrottleTime)) {
256
- yield this.sendSetupContentToUser(setupDetails);
257
- yield this.updateSetupContentSentTime(setupDetails);
258
- sentContent = true;
259
- }
186
+ async initializeNewUser(input) {
187
+ const { uid, email, phone, sendSetupContent, sendSetupContentIfUserExists, data, sendDetailsInTestEnvironment } = input;
188
+ let userRecordPromise;
189
+ if (uid) {
190
+ userRecordPromise = this.authService.auth.getUser(uid);
191
+ }
192
+ else if (email) {
193
+ userRecordPromise = this.authService.auth.getUserByEmail(email);
194
+ }
195
+ else if (phone) {
196
+ userRecordPromise = this.authService.auth.getUserByPhoneNumber(phone);
197
+ }
198
+ else {
199
+ throw new Error('email or phone is required to initialize a new user.');
200
+ }
201
+ let userRecord = await (0, auth_util_1.getAuthUserOrUndefined)(userRecordPromise);
202
+ let userRecordId;
203
+ let createdUser = false;
204
+ if (!userRecord) {
205
+ const createResult = await this.createNewUser(input);
206
+ // add the setup password to the user's credentials
207
+ const userContext = this.authService.userContext(createResult.user.uid);
208
+ await this.addNewUserSetupClaims(userContext, createResult.password);
209
+ createdUser = true;
210
+ userRecordId = userContext.uid;
211
+ userRecord = await userContext.loadRecord();
212
+ }
213
+ else {
214
+ userRecordId = userRecord.uid;
215
+ }
216
+ // send content if necessary
217
+ if ((createdUser && sendSetupContent === true) || sendSetupContentIfUserExists) {
218
+ const sentEmail = await this.sendSetupContent(userRecordId, { data, sendDetailsInTestEnvironment });
219
+ // reload the user record
220
+ if (sentEmail) {
221
+ const userContext = this.authService.userContext(userRecordId);
222
+ userRecord = await userContext.loadRecord();
260
223
  }
261
- return sentContent;
262
- });
224
+ }
225
+ return userRecord;
263
226
  }
264
- loadSetupDetails(userContextOrUid, config) {
265
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
266
- const userContext = userContextFromUid(this.authService, userContextOrUid);
267
- const userExists = yield userContext.exists();
268
- let details;
269
- if (userExists) {
270
- details = yield this.loadSetupDetailsForUserContext(userContext, config);
271
- }
272
- return details;
227
+ async addNewUserSetupClaims(userContextOrUid, setupPassword) {
228
+ const password = setupPassword ?? this.generateRandomSetupPassword();
229
+ const userContext = userContextFromUid(this.authService, userContextOrUid);
230
+ await userContext.updateClaims({
231
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_SETUP_PASSWORD_KEY]: password
273
232
  });
274
- }
275
- loadSetupDetailsForUserContext(userContext, config) {
276
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
277
- let details;
278
- const { setupPassword, setupCommunicationAt } = yield userContext.loadClaims();
279
- if (setupPassword) {
280
- details = {
281
- userContext,
282
- claims: {
283
- setupPassword,
284
- setupCommunicationAt
285
- },
286
- data: config === null || config === void 0 ? void 0 : config.data,
287
- sendDetailsInTestEnvironment: config === null || config === void 0 ? void 0 : config.sendDetailsInTestEnvironment
288
- };
233
+ return userContext;
234
+ }
235
+ async sendSetupContent(userContextOrUid, config) {
236
+ const setupDetails = await this.loadSetupDetails(userContextOrUid, config);
237
+ let sentContent = false;
238
+ if (setupDetails) {
239
+ const { setupCommunicationAt } = setupDetails.claims;
240
+ if (!setupCommunicationAt || (0, date_1.timeHasExpired)(new Date(setupCommunicationAt), this.setupThrottleTime)) {
241
+ await this.sendSetupContentToUser(setupDetails);
242
+ await this.updateSetupContentSentTime(setupDetails);
243
+ sentContent = true;
289
244
  }
290
- return details;
291
- });
245
+ }
246
+ return sentContent;
247
+ }
248
+ async loadSetupDetails(userContextOrUid, config) {
249
+ const userContext = userContextFromUid(this.authService, userContextOrUid);
250
+ const userExists = await userContext.exists();
251
+ let details;
252
+ if (userExists) {
253
+ details = await this.loadSetupDetailsForUserContext(userContext, config);
254
+ }
255
+ return details;
256
+ }
257
+ async loadSetupDetailsForUserContext(userContext, config) {
258
+ let details;
259
+ const { setupPassword, setupCommunicationAt } = await userContext.loadClaims();
260
+ if (setupPassword) {
261
+ details = {
262
+ userContext,
263
+ claims: {
264
+ setupPassword,
265
+ setupCommunicationAt
266
+ },
267
+ data: config?.data,
268
+ sendDetailsInTestEnvironment: config?.sendDetailsInTestEnvironment
269
+ };
270
+ }
271
+ return details;
292
272
  }
293
- updateSetupContentSentTime(details) {
294
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
295
- yield details.userContext.updateClaims({
296
- setupCommunicationAt: (0, date_1.toISODateString)(new Date())
297
- });
273
+ async updateSetupContentSentTime(details) {
274
+ await details.userContext.updateClaims({
275
+ setupCommunicationAt: (0, date_1.toISODateString)(new Date())
298
276
  });
299
277
  }
300
278
  /**
@@ -304,51 +282,43 @@ class AbstractFirebaseServerNewUserService {
304
282
  *
305
283
  * @param uid
306
284
  */
307
- markUserSetupAsComplete(uid) {
308
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
309
- const userContext = this.authService.userContext(uid);
310
- const userExists = yield userContext.exists();
311
- if (userExists) {
312
- yield this.updateClaimsToClearUser(userContext);
313
- }
314
- return userExists;
315
- });
316
- }
317
- createNewUser(input) {
318
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
319
- const { uid, displayName, email, phone: phoneNumber, setupPassword: inputPassword } = input;
320
- const password = inputPassword !== null && inputPassword !== void 0 ? inputPassword : this.generateRandomSetupPassword();
321
- const user = yield this.authService.auth.createUser({
322
- uid,
323
- displayName,
324
- email,
325
- phoneNumber,
326
- password
327
- });
328
- return {
329
- user,
330
- password
331
- };
285
+ async markUserSetupAsComplete(uid) {
286
+ const userContext = this.authService.userContext(uid);
287
+ const userExists = await userContext.exists();
288
+ if (userExists) {
289
+ await this.updateClaimsToClearUser(userContext);
290
+ }
291
+ return userExists;
292
+ }
293
+ async createNewUser(input) {
294
+ const { uid, displayName, email, phone: phoneNumber, setupPassword: inputPassword } = input;
295
+ const password = inputPassword ?? this.generateRandomSetupPassword();
296
+ const user = await this.authService.auth.createUser({
297
+ uid,
298
+ displayName,
299
+ email,
300
+ phoneNumber,
301
+ password
332
302
  });
303
+ return {
304
+ user,
305
+ password
306
+ };
333
307
  }
334
308
  generateRandomSetupPassword() {
335
309
  return `${(0, exports.DEFAULT_FIREBASE_PASSWORD_NUMBER_GENERATOR)()}`;
336
310
  }
337
- updateClaimsToClearUser(userContext) {
338
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
339
- yield userContext.updateClaims({
340
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_SETUP_PASSWORD_KEY]: null,
341
- [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_SETUP_LAST_COM_DATE_KEY]: null
342
- });
311
+ async updateClaimsToClearUser(userContext) {
312
+ await userContext.updateClaims({
313
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_SETUP_PASSWORD_KEY]: null,
314
+ [firebase_1.FIREBASE_SERVER_AUTH_CLAIMS_SETUP_LAST_COM_DATE_KEY]: null
343
315
  });
344
316
  }
345
317
  }
346
318
  exports.AbstractFirebaseServerNewUserService = AbstractFirebaseServerNewUserService;
347
319
  class NoSetupContentFirebaseServerNewUserService extends AbstractFirebaseServerNewUserService {
348
- sendSetupContentToUser(user) {
349
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
350
- // send nothing.
351
- });
320
+ async sendSetupContentToUser(user) {
321
+ // send nothing.
352
322
  }
353
323
  }
354
324
  exports.NoSetupContentFirebaseServerNewUserService = NoSetupContentFirebaseServerNewUserService;
@@ -362,6 +332,7 @@ exports.FirebaseServerAuthService = FirebaseServerAuthService;
362
332
  * Abstract FirebaseServerAuthService implementation.
363
333
  */
364
334
  class AbstractFirebaseServerAuthService {
335
+ auth;
365
336
  constructor(auth) {
366
337
  this.auth = auth;
367
338
  }