backend-manager 3.0.49 → 3.0.50

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.49",
3
+ "version": "3.0.50",
4
4
  "description": "Quick tools for developing Firebase functions",
5
5
  "main": "src/manager/index.js",
6
6
  "bin": {
@@ -2,7 +2,7 @@ const _ = require('lodash')
2
2
  const fetch = require('node-fetch');
3
3
 
4
4
  const MAX_SIGNUPS = 3;
5
- const MAX_AGE = 3;
5
+ const MAX_AGE = 30;
6
6
 
7
7
  function Module() {
8
8
 
@@ -28,11 +28,11 @@ Module.prototype.main = function () {
28
28
  return reject(assistant.errorManager(`Failed to get auth user: ${authUser}`, {code: 500, sentry: false, send: false, log: false}).error)
29
29
  }
30
30
 
31
- // Difference in minutes
32
- const ageInMinutes = (Date.now() - new Date(authUser.metadata.creationTime)) / 1000 / 60;
31
+ // Age in seconds
32
+ const ageInSeconds = (Date.now() - new Date(authUser.metadata.creationTime)) / 1000;
33
33
 
34
34
  // If the user is not new, reject
35
- if (ageInMinutes >= MAX_AGE) {
35
+ if (ageInSeconds >= MAX_AGE) {
36
36
  return reject(assistant.errorManager(`User is not new.`, {code: 400, sentry: false, send: false, log: false}).error)
37
37
  }
38
38
 
@@ -41,14 +41,14 @@ let Module = {
41
41
  if (!payload.title || !payload.body) {
42
42
  response.status = 400;
43
43
  response.error = new Error('Not enough notification parameters supplied.');
44
- assistant.error(response.error, { environment: 'production' })
44
+ assistant.error(response.error)
45
45
  return res.status(response.status).send(response.error.message);
46
46
  }
47
47
 
48
48
  if (!user.roles.admin) {
49
49
  response.status = 401;
50
50
  response.error = new Error('Unauthenticated, admin required.');
51
- assistant.error(response.error, { environment: 'production' })
51
+ assistant.error(response.error)
52
52
  return res.status(response.status).send(response.error.message);
53
53
  } else {
54
54
  await self.getTokens({tags: false});
@@ -29,10 +29,10 @@ Module.prototype.main = function () {
29
29
  const admin = self.libraries.admin;
30
30
  const storage = self.Manager.storage({ temporary: true, name: 'rate-limiting' });
31
31
 
32
- assistant.log(`Request: ${user.uid}`, user, context, { environment: 'production' });
32
+ assistant.log(`Request: ${user.uid}`, user, context);
33
33
 
34
34
  // if (context.additionalUserInfo.recaptchaScore < 0.5) {
35
- // assistant.error(`Recaptcha score (${context.additionalUserInfo.recaptchaScore}) too low for ${user.uid}`, { environment: 'production' });
35
+ // assistant.error(`Recaptcha score (${context.additionalUserInfo.recaptchaScore}) too low for ${user.uid}`);
36
36
 
37
37
  // throw new functions.auth.HttpsError('resource-exhausted', ERROR_TOO_MANY_ATTEMPTS);
38
38
  // }
@@ -46,10 +46,10 @@ Module.prototype.main = function () {
46
46
  const count = get(rateLimitingData, 'count', 0);
47
47
  const lastTime = get(rateLimitingData, 'lastTime', 0);
48
48
 
49
- assistant.log(`Rate limiting for ${ipAddress}:`, rateLimitingData, { environment: 'production' });
49
+ assistant.log(`Rate limiting for ${ipAddress}:`, rateLimitingData);
50
50
 
51
51
  if (currentTime - lastTime < oneHour && count >= 2) {
52
- assistant.error(`Too many attemps to create an account for ${ipAddress}`, { environment: 'production' });
52
+ assistant.error(`Too many attemps to create an account for ${ipAddress}`);
53
53
 
54
54
  throw new functions.auth.HttpsError('resource-exhausted', ERROR_TOO_MANY_ATTEMPTS);
55
55
  }
@@ -64,7 +64,7 @@ Module.prototype.main = function () {
64
64
 
65
65
  // If user already exists, skip auth-on-create handler
66
66
  if (existingAccount instanceof Error) {
67
- assistant.error(`Failed to get existing account ${user.uid}:`, existingAccount, { environment: 'production' });
67
+ assistant.error(`Failed to get existing account ${user.uid}:`, existingAccount);
68
68
 
69
69
  throw new functions.auth.HttpsError('internal', `Failed to get existing account: ${existingAccount}`);
70
70
  }
@@ -109,12 +109,12 @@ Module.prototype.main = function () {
109
109
  .set(account, { merge: true });
110
110
 
111
111
  if (update instanceof Error) {
112
- assistant.error(`Failed to update user ${user.uid}:`, update, { environment: 'production' });
112
+ assistant.error(`Failed to update user ${user.uid}:`, update);
113
113
 
114
114
  throw new functions.auth.HttpsError('internal', `Failed to update user: ${update}`);
115
115
  }
116
116
 
117
- assistant.log(`User created at users/${user.uid}`, account, { environment: 'production' });
117
+ assistant.log(`User created at users/${user.uid}`, account);
118
118
 
119
119
  return resolve(self);
120
120
  });
@@ -49,7 +49,7 @@ Module.prototype.main = function () {
49
49
  }, { merge: true });
50
50
 
51
51
  if (update instanceof Error) {
52
- assistant.error(`Failed to update user ${user.uid}:`, update, { environment: 'production' });
52
+ assistant.error(`Failed to update user ${user.uid}:`, update);
53
53
 
54
54
  throw new functions.auth.HttpsError('internal', `Failed to update user: ${update}`);
55
55
  }
@@ -1,4 +1,7 @@
1
1
  const { get, merge } = require('lodash');
2
+ const powertools = require('node-powertools');
3
+
4
+ const MAX_AGE = 30;
2
5
 
3
6
  function Module() {
4
7
  const self = this;
@@ -23,18 +26,36 @@ Module.prototype.main = function () {
23
26
  const context = self.context;
24
27
 
25
28
  return new Promise(async function(resolve, reject) {
26
- assistant.log(`Request: ${user.uid}`, user, context, { environment: 'production' });
29
+ assistant.log(`Request: ${user.uid}`, user, context);
30
+
31
+ const ageInSeconds = (Date.now() - new Date(user.metadata.creationTime)) / 1000;
27
32
 
28
33
  // Check if exists already
29
34
  // It could exist already if user signed up with email and then signed in with Google
30
- const existingUser = await libraries.admin.firestore().doc(`users/${user.uid}`)
31
- .get()
32
- .then((doc) => doc.data() || {})
33
- .catch(e => e)
35
+ let existingUser;
36
+ await powertools.poll(async () => {
37
+ assistant.log(`Polling for existing user ${user.uid}...`);
38
+ existingUser = await libraries.admin.firestore().doc(`users/${user.uid}`)
39
+ .get()
40
+ .then((doc) => doc.data())
41
+ .catch(e => e);
42
+
43
+ return existingUser && !(existingUser instanceof Error);
44
+ }, {interval: 1000, timeout: 30000})
45
+ .catch(e => {
46
+ existingUser = e;
47
+ assistant.error(`Timeout for existing user expired`, e);
48
+ });
49
+
50
+ assistant.log(`Existing user ${user.uid} found (age=${ageInSeconds}):`, existingUser);
51
+
52
+ if (ageInSeconds >= MAX_AGE) {
53
+ existingUser = new Error(`User is not new (age=${ageInSeconds}).`);
54
+ }
34
55
 
35
56
  // If user already exists, skip auth-on-create handler
36
57
  if (existingUser instanceof Error) {
37
- assistant.error(`Failed to get existing user ${user.uid}:`, existingUser, { environment: 'production' });
58
+ assistant.error(`Failed to get existing user ${user.uid}:`, existingUser);
38
59
 
39
60
  return reject(existingUser);
40
61
  }
@@ -21,7 +21,7 @@ Module.prototype.main = function () {
21
21
  const context = self.context;
22
22
 
23
23
  return new Promise(async function(resolve, reject) {
24
- assistant.log(`Request: ${user.uid}`, user, context, { environment: 'production' });
24
+ assistant.log(`Request: ${user.uid}`, user, context);
25
25
 
26
26
  // Set up analytics
27
27
  const analytics = self.Manager.Analytics({