backend-manager 3.0.46 → 3.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-manager",
3
- "version": "3.0.46",
3
+ "version": "3.0.48",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -14,23 +14,21 @@ Module.prototype.main = function () {
14
14
  return new Promise(async function(resolve, reject) {
15
15
  self.Api.resolveUser({adminRequired: true})
16
16
  .then(async (user) => {
17
- // const planExpireDate = new Date(_.get(payload.user, 'plan.expires.timestamp', 0));
18
- // if (planExpireDate >= new Date()) {
19
- // payload.response.status = 401;
20
- // payload.response.error = new Error(`Failed to delete user: There is an active paid subscription on this account. Please cancel it first and then try deleting the account again.`);
21
- // return reject(payload.response.error);
22
- // }
23
-
24
17
  // Disallow deleting users with subscriptions in any state other than cancelled or active payments
25
18
  if (
26
19
  (user?.plan?.status && user?.plan?.status !== 'cancelled')
27
20
  || user?.plan?.payment?.active
28
21
  ) {
29
- // return reject(assistant.errorManager(`Failed to delete user: There is an active paid subscription on this account. Please cancel it first and then try deleting the account again.`, {code: 400, sentry: false, send: false, log: false}).error)
30
- // return reject(assistant.errorManager(`This account cannot be deleted until the paid subscription attached to it is cancelled. Please cancel the subscription and then try to delete the account.`, {code: 400, sentry: false, send: false, log: false}).error)
31
22
  return reject(assistant.errorManager(`This account cannot be deleted because it has a paid subscription attached to it. In order to delete the account, you must first cancel the paid subscription.`, {code: 400, sentry: false, send: false, log: false}).error)
32
23
  }
33
24
 
25
+ // Signout of all sessions
26
+ await Api.import('user:sign-out-all-sessions')
27
+ .then(async (lib) => {
28
+ await lib.main().catch(e => e);
29
+ })
30
+
31
+ // Perform the delete
34
32
  await self.libraries.admin.auth().deleteUser(_.get(user, 'auth.uid', null))
35
33
  .then(() => {
36
34
  return resolve({data: {success: true}});
@@ -17,20 +17,37 @@ Module.prototype.main = function () {
17
17
  self.Api.resolveUser({adminRequired: true})
18
18
  .then(async (user) => {
19
19
  // Get auth user from firebase
20
- const authUser = await self.libraries.admin.auth().getUser(user.auth.uid).catch(e => e);
20
+ const ip = assistant.request.geolocation.ip;
21
+ const authUser = await Manager.libraries.admin.auth().getUser(user.auth.uid).catch(e => e);
22
+ const usage = await Manager.Usage().init(assistant, {log: true, localKey: ip});
21
23
 
22
24
  if (authUser instanceof Error) {
23
25
  return reject(assistant.errorManager(`Failed to get auth user: ${authUser}`, {code: 500, sentry: false, send: false, log: false}).error)
24
26
  }
25
27
 
26
- // Difference in hours
27
- const diff = (Date.now() - new Date(authUser.metadata.creationTime)) / 36e5;
28
+ // Difference in minutes
29
+ const ageInMinutes = (Date.now() - new Date(authUser.metadata.creationTime)) / 1000 / 60;
28
30
 
29
31
  // If the user is not new, reject
30
- if (diff > 0.5) {
32
+ if (ageInMinutes > 3) {
31
33
  return reject(assistant.errorManager(`User is not new.`, {code: 400, sentry: false, send: false, log: false}).error)
32
34
  }
33
35
 
36
+ // Check if IP has signed up too many times
37
+ const signups = usage.getUsage('signups');
38
+
39
+ // If over limit, reject and delete the user
40
+ if (signups >= 3) {
41
+ await Api.import('user:delete')
42
+ .then(async (lib) => {
43
+ await lib.main().catch(e => e);
44
+ })
45
+ return reject(assistant.errorManager(`Too many signups from this IP (${ip}).`, {code: 429, sentry: false, send: false, log: false}).error)
46
+ }
47
+
48
+ // Increment signups
49
+ usage.increment('signups');
50
+
34
51
  await self.signUp({
35
52
  auth: {
36
53
  uid: _.get(user, 'auth.uid', null),
@@ -78,7 +95,7 @@ Module.prototype.signUp = function (payload) {
78
95
 
79
96
  const result = {
80
97
  signedUp: false,
81
- referrerUid: undefined,
98
+ referrerUid: null,
82
99
  // updatedReferral: true,
83
100
  };
84
101
 
@@ -146,7 +163,7 @@ Module.prototype.updateReferral = function (payload) {
146
163
  const result = {
147
164
  count: 0,
148
165
  updatedReferral: false,
149
- referrerUid: undefined,
166
+ referrerUid: null,
150
167
  }
151
168
  payload = payload || {};
152
169
 
@@ -181,7 +198,7 @@ Module.prototype.updateReferral = function (payload) {
181
198
  await self.libraries.admin.firestore().doc(`users/${doc.ref.id}`)
182
199
  .set({
183
200
  affiliate: {
184
- referrals: referrals
201
+ referrals: referrals,
185
202
  }
186
203
  }, {merge: true})
187
204
  .catch(e => {
@@ -192,7 +209,7 @@ Module.prototype.updateReferral = function (payload) {
192
209
 
193
210
  result.count = count;
194
211
  result.updatedReferral = true;
195
- result.referrerUid = doc.ref.id
212
+ result.referrerUid = doc.ref.id;
196
213
  found = true
197
214
  }
198
215
  }
@@ -1,4 +1,4 @@
1
- const { get } = require('lodash');
1
+ const { get, merge } = require('lodash');
2
2
 
3
3
  function Module() {
4
4
  const self = this;
@@ -37,22 +37,17 @@ Module.prototype.main = function () {
37
37
  assistant.error(`Failed to get existing user ${user.uid}:`, existingUser, { environment: 'production' });
38
38
 
39
39
  return reject(existingUser);
40
- } else if (
41
- get(existingUser, 'auth.uid', null)
42
- || get(existingUser, 'auth.email', null)
43
- ) {
44
- assistant.log(`Skipping handler because user already exists ${user.uid}:`, existingUser);
45
-
46
- return resolve(self);
47
40
  }
48
41
 
49
42
  // Build user object
50
- const newUser = self.Manager.User({
43
+ let newUser = self.Manager.User().properties;
44
+
45
+ newUser = merge(newUser, existingUser, {
51
46
  auth: {
52
47
  uid: user.uid,
53
48
  email: user.email,
54
- }
55
- }).properties;
49
+ },
50
+ });
56
51
 
57
52
  // Set up analytics
58
53
  const analytics = self.Manager.Analytics({
@@ -128,7 +128,7 @@ Usage.prototype.validate = function (path, options) {
128
128
  options.useCaptchaResponse = typeof options.useCaptchaResponse === 'undefined' ? true : options.useCaptchaResponse;
129
129
 
130
130
  // Check for required options
131
- const period = self.getUsage(path)
131
+ const period = self.getUsage(path);
132
132
  const allowed = self.getLimit(path);
133
133
 
134
134
  // Log
@@ -238,8 +238,11 @@ Usage.prototype.update = function () {
238
238
  const Manager = self.Manager;
239
239
  const assistant = self.assistant;
240
240
 
241
- // Write self.user to firestore or local if no user
242
- if (self.user.auth.uid) {
241
+ // Write self.user to firestore or local if no user or if localKey is set
242
+ if (
243
+ self.user.auth.uid
244
+ && !self.options.localKey
245
+ ) {
243
246
  Manager.libraries.admin.firestore().doc(`users/${self.user.auth.uid}`)
244
247
  .set({
245
248
  usage: self.user.usage,