@como/como-client 0.9.2 → 0.9.4
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/lib/como.js +355 -431
- package/lib/services/auth.service.js +5 -38
- package/package.json +1 -1
- package/src/como.js +37 -51
|
@@ -401,19 +401,19 @@ exports.logOut = function (service) {
|
|
|
401
401
|
}));
|
|
402
402
|
};
|
|
403
403
|
|
|
404
|
-
exports.
|
|
404
|
+
exports.refreshToken = function (service) {
|
|
405
405
|
return function () {
|
|
406
|
-
var _ref23 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(
|
|
406
|
+
var _ref23 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee12(uid, token, locationId, source) {
|
|
407
407
|
var params, headers, _ref24, data;
|
|
408
408
|
|
|
409
409
|
return _regenerator2.default.wrap(function _callee12$(_context12) {
|
|
410
410
|
while (1) {
|
|
411
411
|
switch (_context12.prev = _context12.next) {
|
|
412
412
|
case 0:
|
|
413
|
-
params = {
|
|
413
|
+
params = { uid: uid, refreshToken: token, locationId: locationId, source: source };
|
|
414
414
|
headers = getHashHeader(params);
|
|
415
415
|
_context12.next = 4;
|
|
416
|
-
return service.post('/auth/
|
|
416
|
+
return service.post('/auth/v2/auth/refresh-token', params, { headers: headers });
|
|
417
417
|
|
|
418
418
|
case 4:
|
|
419
419
|
_ref24 = _context12.sent;
|
|
@@ -428,41 +428,8 @@ exports.socialLogin = function (service) {
|
|
|
428
428
|
}, _callee12, undefined);
|
|
429
429
|
}));
|
|
430
430
|
|
|
431
|
-
return function (_x39, _x40, _x41) {
|
|
431
|
+
return function (_x39, _x40, _x41, _x42) {
|
|
432
432
|
return _ref23.apply(this, arguments);
|
|
433
433
|
};
|
|
434
434
|
}();
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
exports.refreshToken = function (service) {
|
|
438
|
-
return function () {
|
|
439
|
-
var _ref25 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee13(uid, token, locationId, source) {
|
|
440
|
-
var params, headers, _ref26, data;
|
|
441
|
-
|
|
442
|
-
return _regenerator2.default.wrap(function _callee13$(_context13) {
|
|
443
|
-
while (1) {
|
|
444
|
-
switch (_context13.prev = _context13.next) {
|
|
445
|
-
case 0:
|
|
446
|
-
params = { uid: uid, refreshToken: token, locationId: locationId, source: source };
|
|
447
|
-
headers = getHashHeader(params);
|
|
448
|
-
_context13.next = 4;
|
|
449
|
-
return service.post('/auth/v2/auth/refresh-token', params, { headers: headers });
|
|
450
|
-
|
|
451
|
-
case 4:
|
|
452
|
-
_ref26 = _context13.sent;
|
|
453
|
-
data = _ref26.data;
|
|
454
|
-
return _context13.abrupt("return", data);
|
|
455
|
-
|
|
456
|
-
case 7:
|
|
457
|
-
case "end":
|
|
458
|
-
return _context13.stop();
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}, _callee13, undefined);
|
|
462
|
-
}));
|
|
463
|
-
|
|
464
|
-
return function (_x42, _x43, _x44, _x45) {
|
|
465
|
-
return _ref25.apply(this, arguments);
|
|
466
|
-
};
|
|
467
|
-
}();
|
|
468
435
|
};
|
package/package.json
CHANGED
package/src/como.js
CHANGED
|
@@ -27,8 +27,7 @@ class Como {
|
|
|
27
27
|
const { refresher, expiresIn } = this.comoData;
|
|
28
28
|
if (!refresher) {
|
|
29
29
|
console.log('No refresh for %o', this.comoData);
|
|
30
|
-
}
|
|
31
|
-
else if (expiresIn && moment(expiresIn).subtract(60, 'seconds').isBefore(moment())) {
|
|
30
|
+
} else if (expiresIn && moment(expiresIn).subtract(60, 'seconds').isBefore(moment())) {
|
|
32
31
|
await refresher();
|
|
33
32
|
}
|
|
34
33
|
}
|
|
@@ -70,7 +69,9 @@ class Como {
|
|
|
70
69
|
*/
|
|
71
70
|
async init(environment, credentials, refreshCallback, enableSignon = true) {
|
|
72
71
|
const isExists = (x => !ramda.isNil(x) && !ramda.isEmpty(x));
|
|
73
|
-
const {
|
|
72
|
+
const {
|
|
73
|
+
id: environmentId, locationId, installationId, source, binaryVersion, phoenixVersion,
|
|
74
|
+
} = environment;
|
|
74
75
|
if (!isExists(environmentId) || !isExists(locationId) || !isExists(installationId)) {
|
|
75
76
|
throw new Error(`missing parameters: ${environment}`);
|
|
76
77
|
}
|
|
@@ -105,18 +106,20 @@ class Como {
|
|
|
105
106
|
|
|
106
107
|
const { accessToken, refreshToken, expiresIn } = credentials;
|
|
107
108
|
let credExist = isExists(accessToken) && isExists(refreshToken);
|
|
108
|
-
|
|
109
|
+
const isExpired = credExist && expiresIn && moment(expiresIn).isBefore(moment());
|
|
109
110
|
let refreshFailed = false;
|
|
110
111
|
if (isExpired) {
|
|
111
112
|
try {
|
|
112
|
-
const data = await this.comoData.initApi.refreshToken(installationId, refreshToken
|
|
113
|
+
const data = await this.comoData.initApi.refreshToken(installationId, refreshToken || 'none', locationId, source);
|
|
113
114
|
this.comoData.config = data.config;
|
|
114
115
|
console.log(`signOn with isExpired true result:${JSON.stringify(data)}`);
|
|
115
116
|
updateCredentials(data, true);
|
|
116
117
|
} catch (e) {
|
|
117
118
|
console.error(`Failed to refresh token, installationId:${installationId}, refreshToken:${this.comoData.refreshToken}, locationId:${locationId}, source:${source}`);
|
|
118
|
-
//refresh failed - need to revoke and signOn again
|
|
119
|
-
initService.logError({
|
|
119
|
+
// refresh failed - need to revoke and signOn again
|
|
120
|
+
initService.logError({
|
|
121
|
+
method: 'refreshToken', installationId, locationId, source, refreshToken, error: e.message,
|
|
122
|
+
});
|
|
120
123
|
refreshFailed = true;
|
|
121
124
|
}
|
|
122
125
|
}
|
|
@@ -132,12 +135,13 @@ class Como {
|
|
|
132
135
|
} catch (e) {
|
|
133
136
|
console.error(`Failed to signOn, installationId:${installationId}, locationId:${locationId}, source:${source},interval:${interval}, error:${e}`);
|
|
134
137
|
interval += 1000;
|
|
135
|
-
initService.logError({
|
|
138
|
+
initService.logError({
|
|
139
|
+
method: 'signOn no accessToken', accessToken, isExpired, refreshFailed, credExist, installationId, locationId, source, refreshToken, interval, error: e.message,
|
|
140
|
+
});
|
|
136
141
|
await new Promise(r => setTimeout(r, interval));
|
|
137
142
|
}
|
|
138
143
|
}
|
|
139
|
-
}
|
|
140
|
-
else if (!isExpired) {
|
|
144
|
+
} else if (!isExpired) {
|
|
141
145
|
let signOnSuccessfully = false;
|
|
142
146
|
while (!signOnSuccessfully && interval < 3000) {
|
|
143
147
|
try {
|
|
@@ -146,7 +150,7 @@ class Como {
|
|
|
146
150
|
this.comoData.config = data.config;
|
|
147
151
|
console.log(`signOn with result " ${JSON.stringify(data)}`);
|
|
148
152
|
if (data && data.accessToken) {
|
|
149
|
-
console.log(
|
|
153
|
+
console.log('signOn- updating credentials');
|
|
150
154
|
updateCredentials(data, true);
|
|
151
155
|
}
|
|
152
156
|
if (data && data.config && data.config.recaptchaSiteKey) {
|
|
@@ -156,7 +160,9 @@ class Como {
|
|
|
156
160
|
} catch (e) {
|
|
157
161
|
console.error(`Failed signOn with accessToken, installationId:${installationId}, locationId:${locationId}, source:${source},accessToken:${accessToken}, error:${e.message}`);
|
|
158
162
|
interval += 1000;
|
|
159
|
-
initService.logError({
|
|
163
|
+
initService.logError({
|
|
164
|
+
method: 'signOn with credentials', accessToken, isExpired, refreshFailed, credExist, installationId, locationId, source, refreshToken, interval, error: e.message,
|
|
165
|
+
});
|
|
160
166
|
await new Promise(r => setTimeout(r, interval));
|
|
161
167
|
}
|
|
162
168
|
}
|
|
@@ -385,39 +391,18 @@ class Como {
|
|
|
385
391
|
return data;
|
|
386
392
|
} catch (error) {
|
|
387
393
|
if (error.response && error.response.data) {
|
|
388
|
-
const { code, message } = error.response.data;
|
|
389
|
-
|
|
394
|
+
const { code, message, retryAfter } = error.response.data;
|
|
395
|
+
const loginError = new Error(code || message);
|
|
396
|
+
// When a request lands inside its OTP backoff the server states how long
|
|
397
|
+
// to wait. Only code and message used to survive this rethrow, which left
|
|
398
|
+
// the app inventing a countdown of its own.
|
|
399
|
+
if (typeof retryAfter === 'number') { loginError.retryAfter = retryAfter; }
|
|
400
|
+
throw loginError;
|
|
390
401
|
}
|
|
391
402
|
throw error;
|
|
392
403
|
}
|
|
393
404
|
}
|
|
394
405
|
|
|
395
|
-
/**
|
|
396
|
-
* Social login via Firebase SSO (Google/Facebook/Apple).
|
|
397
|
-
* @param {Object} params
|
|
398
|
-
* @param {string} params.socialId - Firebase user UID
|
|
399
|
-
* @param {string} params.provider - Social provider (google/facebook/apple)
|
|
400
|
-
* @param {string} params.idToken - Firebase ID token
|
|
401
|
-
* @returns {Promise<Object>} { success, accessToken, refreshToken, consent } or { success: false, userExists: false }
|
|
402
|
-
*/
|
|
403
|
-
async socialLogin({ socialId, provider, idToken }) {
|
|
404
|
-
const { api } = this.comoData;
|
|
405
|
-
try {
|
|
406
|
-
const data = await api.socialLogin(socialId, provider, idToken);
|
|
407
|
-
return data;
|
|
408
|
-
} catch (error) {
|
|
409
|
-
if (error.response && error.response.data) {
|
|
410
|
-
const { code, message } = error.response.data;
|
|
411
|
-
throw new Error(code || message);
|
|
412
|
-
}
|
|
413
|
-
throw error;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
async socialLoginCheck({ socialId, provider, idToken }) {
|
|
418
|
-
return this.socialLogin({ socialId, provider, idToken });
|
|
419
|
-
}
|
|
420
|
-
|
|
421
406
|
/**
|
|
422
407
|
*
|
|
423
408
|
* @param {Object} identifier key-value identifiers
|
|
@@ -434,11 +419,14 @@ class Como {
|
|
|
434
419
|
this.comoData.consent = consent;
|
|
435
420
|
} catch (error) {
|
|
436
421
|
if (error.response && error.response.data) {
|
|
437
|
-
const { code: errCode, message } = error.response.data;
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
422
|
+
const { code: errCode, message, retryAfter } = error.response.data;
|
|
423
|
+
// The wrong-code case keeps reporting the message rather than the code,
|
|
424
|
+
// since callers match on that string.
|
|
425
|
+
const verifyError = (message && message === 'Invalid verification Code')
|
|
426
|
+
? new Error(message)
|
|
427
|
+
: new Error(errCode || message);
|
|
428
|
+
if (typeof retryAfter === 'number') { verifyError.retryAfter = retryAfter; }
|
|
429
|
+
throw verifyError;
|
|
442
430
|
}
|
|
443
431
|
throw error;
|
|
444
432
|
}
|
|
@@ -482,10 +470,8 @@ class Como {
|
|
|
482
470
|
} = this.comoData;
|
|
483
471
|
try {
|
|
484
472
|
return api.clientUpdateMembership(env.services_root, locationId, pushEnable, locationEnable);
|
|
485
|
-
}
|
|
486
|
-
catch (e) {
|
|
473
|
+
} catch (e) {
|
|
487
474
|
console.log(`clientUpdateMembership failed, token:${token}, locationId:${locationId}, error: ${e}`);
|
|
488
|
-
|
|
489
475
|
}
|
|
490
476
|
}
|
|
491
477
|
|
|
@@ -508,7 +494,7 @@ class Como {
|
|
|
508
494
|
resource.id,
|
|
509
495
|
('Namespace' in resource) ? resource.Namespace : `location_${this.comoData.locationId}`,
|
|
510
496
|
resource.Version,
|
|
511
|
-
this.comoData.locationId
|
|
497
|
+
this.comoData.locationId,
|
|
512
498
|
);
|
|
513
499
|
|
|
514
500
|
this.comoData.mainResources[resourceId] = responseJson;
|
|
@@ -863,7 +849,7 @@ class Como {
|
|
|
863
849
|
getFriendReferralInviteLink(referral) {
|
|
864
850
|
const { env_params: env, locationId } = this.comoData;
|
|
865
851
|
const cpBaseUrl = env.cp_base_url.replace('{location_id}', locationId);
|
|
866
|
-
return `${cpBaseUrl}/auth/
|
|
852
|
+
return `${cpBaseUrl}/auth/signin?onboardingflow=register&ReferralCode=${referral}`;
|
|
867
853
|
}
|
|
868
854
|
|
|
869
855
|
/**
|
|
@@ -1072,7 +1058,7 @@ class Como {
|
|
|
1072
1058
|
*/
|
|
1073
1059
|
async getPurchaseHistory(limit, offset) {
|
|
1074
1060
|
const {
|
|
1075
|
-
locationId, api, membership
|
|
1061
|
+
locationId, api, membership,
|
|
1076
1062
|
} = this.comoData;
|
|
1077
1063
|
return api.getPurchaseHistory(locationId, membership.Key, limit, offset);
|
|
1078
1064
|
}
|