@singularlogic/coreplatts 0.0.2 → 0.0.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/dist/index.js +26 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -272,16 +272,30 @@ var BucketConsumer = class extends BaseConsumer {
|
|
|
272
272
|
function withValidToken(fn, accountsAPI) {
|
|
273
273
|
return function(...args) {
|
|
274
274
|
return __async(this, null, function* () {
|
|
275
|
-
|
|
276
|
-
const
|
|
277
|
-
|
|
278
|
-
|
|
275
|
+
var _a;
|
|
276
|
+
const isBrowser = typeof window !== "undefined";
|
|
277
|
+
const storage = isBrowser ? (_a = window.sessionStorage) != null ? _a : window.localStorage : void 0;
|
|
278
|
+
if (!storage) {
|
|
279
|
+
console.warn("\u26A0\uFE0F sessionStorage or localStorage not available. You must pass token manually.");
|
|
279
280
|
return Promise.resolve(fn.apply(this, args));
|
|
280
281
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
282
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
283
|
+
let token = storage.getItem("access_token");
|
|
284
|
+
let refreshToken = storage.getItem("refresh_token");
|
|
285
|
+
let exp = Number(storage.getItem("exp") || "0");
|
|
286
|
+
let refreshExp = Number(storage.getItem("refresh_exp") || "0");
|
|
287
|
+
if ((!exp || !refreshExp) && storage.getItem("id_token_claims_obj")) {
|
|
288
|
+
try {
|
|
289
|
+
const claims = JSON.parse(storage.getItem("id_token_claims_obj"));
|
|
290
|
+
if (claims.exp) exp = claims.exp;
|
|
291
|
+
if (claims.iat && claims.exp) {
|
|
292
|
+
const estimatedDuration = claims.exp - claims.iat;
|
|
293
|
+
refreshExp = claims.iat + 3 * estimatedDuration;
|
|
294
|
+
}
|
|
295
|
+
} catch (err) {
|
|
296
|
+
console.warn("\u26A0\uFE0F Could not parse id_token_claims_obj:", err);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
285
299
|
if (!token || now >= exp) {
|
|
286
300
|
if (!refreshToken || now >= refreshExp) {
|
|
287
301
|
console.warn("\u26A0\uFE0F Session expired. Please log in again.");
|
|
@@ -297,10 +311,10 @@ function withValidToken(fn, accountsAPI) {
|
|
|
297
311
|
const session = yield response.json();
|
|
298
312
|
const newExp = now + session.expires_in;
|
|
299
313
|
const newRefreshExp = now + session.refresh_expires_in;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
314
|
+
storage.setItem("access_token", session.access_token);
|
|
315
|
+
storage.setItem("refresh_token", session.refresh_token);
|
|
316
|
+
storage.setItem("exp", String(newExp));
|
|
317
|
+
storage.setItem("refresh_exp", String(newRefreshExp));
|
|
304
318
|
token = session.access_token;
|
|
305
319
|
} catch (e) {
|
|
306
320
|
console.error("\u{1F510} Token refresh failed:", e);
|