@singularlogic/coreplatts 0.0.23 → 0.0.24
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 +4 -58
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -278,66 +278,12 @@ var FileConsumer = class extends BaseConsumer {
|
|
|
278
278
|
};
|
|
279
279
|
|
|
280
280
|
// src/utils/utils.ts
|
|
281
|
-
function withValidToken(fn,
|
|
281
|
+
function withValidToken(fn, _accountsAPI) {
|
|
282
282
|
return function(...args) {
|
|
283
283
|
return __async(this, null, function* () {
|
|
284
|
-
const
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
console.warn("\u26A0\uFE0F sessionStorage not available");
|
|
288
|
-
return fn.apply(this, args);
|
|
289
|
-
}
|
|
290
|
-
let token = storage.getItem("access_token");
|
|
291
|
-
let refreshToken = storage.getItem("refresh_token");
|
|
292
|
-
let exp = 0;
|
|
293
|
-
let refreshExp = 0;
|
|
294
|
-
const claimsRaw = storage.getItem("id_token_claims_obj");
|
|
295
|
-
if (claimsRaw) {
|
|
296
|
-
try {
|
|
297
|
-
const claims = JSON.parse(claimsRaw);
|
|
298
|
-
if (claims.exp) exp = claims.exp;
|
|
299
|
-
if (claims.iat && claims.exp) {
|
|
300
|
-
const duration = claims.exp - claims.iat;
|
|
301
|
-
refreshExp = claims.iat + 3 * duration;
|
|
302
|
-
}
|
|
303
|
-
} catch (e) {
|
|
304
|
-
console.warn("\u26A0\uFE0F Could not parse id_token_claims_obj:", e);
|
|
305
|
-
}
|
|
306
|
-
} else {
|
|
307
|
-
exp = Number(storage.getItem("refresh_expires_in") || "0");
|
|
308
|
-
refreshExp = Number(storage.getItem("refresh_exp") || "0");
|
|
309
|
-
}
|
|
310
|
-
if (!token || !refreshToken) {
|
|
311
|
-
console.warn("\u26A0\uFE0F No tokens found in sessionStorage.");
|
|
312
|
-
return fn.apply(this, args);
|
|
313
|
-
}
|
|
314
|
-
if (now >= exp) {
|
|
315
|
-
if (now >= refreshExp) {
|
|
316
|
-
console.warn("\u26A0\uFE0F Both access and refresh tokens expired.");
|
|
317
|
-
return fn.apply(this, args);
|
|
318
|
-
}
|
|
319
|
-
try {
|
|
320
|
-
const response = yield fetch(`${accountsAPI}/user/refresh`, {
|
|
321
|
-
method: "POST",
|
|
322
|
-
headers: { "Content-Type": "application/json" },
|
|
323
|
-
body: JSON.stringify({ refresh_token: refreshToken })
|
|
324
|
-
});
|
|
325
|
-
if (!response.ok) throw new Error("Token refresh failed");
|
|
326
|
-
const session = yield response.json();
|
|
327
|
-
token = session.access_token;
|
|
328
|
-
refreshToken = session.refresh_token;
|
|
329
|
-
const newExp = now + session.expires_in;
|
|
330
|
-
const newRefreshExp = now + session.refresh_expires_in;
|
|
331
|
-
storage.setItem("access_token", token);
|
|
332
|
-
storage.setItem("refresh_token", refreshToken);
|
|
333
|
-
storage.setItem("refresh_expires_in", String(newExp));
|
|
334
|
-
storage.setItem("refresh_exp", String(newRefreshExp));
|
|
335
|
-
} catch (err) {
|
|
336
|
-
console.error("\u{1F510} Token refresh failed:", err);
|
|
337
|
-
throw new Error("Could not refresh session.");
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
return yield fn.apply(this, [...args, token]);
|
|
284
|
+
const last = args[args.length - 1];
|
|
285
|
+
const hasToken = typeof last === "string" && last.length > 0;
|
|
286
|
+
return hasToken ? yield fn.apply(this, args) : yield fn.apply(this, args);
|
|
341
287
|
});
|
|
342
288
|
};
|
|
343
289
|
}
|