@singularlogic/coreplatts 0.0.2 → 0.0.3
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 +14 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -272,16 +272,18 @@ 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
|
-
const
|
|
284
|
-
const
|
|
282
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
283
|
+
let token = storage.getItem("token");
|
|
284
|
+
const refreshToken = storage.getItem("refresh_token");
|
|
285
|
+
const exp = Number(storage.getItem("exp") || "0");
|
|
286
|
+
const refreshExp = Number(storage.getItem("refresh_exp") || "0");
|
|
285
287
|
if (!token || now >= exp) {
|
|
286
288
|
if (!refreshToken || now >= refreshExp) {
|
|
287
289
|
console.warn("\u26A0\uFE0F Session expired. Please log in again.");
|
|
@@ -297,10 +299,10 @@ function withValidToken(fn, accountsAPI) {
|
|
|
297
299
|
const session = yield response.json();
|
|
298
300
|
const newExp = now + session.expires_in;
|
|
299
301
|
const newRefreshExp = now + session.refresh_expires_in;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
302
|
+
storage.setItem("token", session.access_token);
|
|
303
|
+
storage.setItem("refresh_token", session.refresh_token);
|
|
304
|
+
storage.setItem("exp", String(newExp));
|
|
305
|
+
storage.setItem("refresh_exp", String(newRefreshExp));
|
|
304
306
|
token = session.access_token;
|
|
305
307
|
} catch (e) {
|
|
306
308
|
console.error("\u{1F510} Token refresh failed:", e);
|