@singularlogic/coreplatts 0.0.5 → 0.0.6
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.d.ts +2 -1
- package/dist/index.js +65 -44
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -155,7 +155,8 @@ declare class Client implements IClient$1 {
|
|
|
155
155
|
private _bucketConsumer;
|
|
156
156
|
private _folderConsumer;
|
|
157
157
|
constructor(_managementURL: string, _accountURL: string);
|
|
158
|
-
|
|
158
|
+
setSession(session: OidcToken): void;
|
|
159
|
+
getSessionVariable(key: string): string | null;
|
|
159
160
|
login(email: string, password: string): Promise<OidcToken>;
|
|
160
161
|
myUser(token?: string): Promise<User$1>;
|
|
161
162
|
allOrganizations(token?: string): Promise<Group$1[]>;
|
package/dist/index.js
CHANGED
|
@@ -272,20 +272,40 @@ var BucketConsumer = class extends BaseConsumer {
|
|
|
272
272
|
function withValidToken(fn, accountsAPI) {
|
|
273
273
|
return function(...args) {
|
|
274
274
|
return __async(this, null, function* () {
|
|
275
|
-
const isStorageAvailable = typeof localStorage !== "undefined";
|
|
276
275
|
const now = Math.floor(Date.now() / 1e3);
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
276
|
+
const storage = typeof window !== "undefined" ? window.sessionStorage : globalThis.sessionStorage;
|
|
277
|
+
if (!storage) {
|
|
278
|
+
console.warn("\u26A0\uFE0F sessionStorage not available");
|
|
279
|
+
return fn.apply(this, args);
|
|
280
280
|
}
|
|
281
|
-
let token =
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
281
|
+
let token = storage.getItem("access_token");
|
|
282
|
+
let refreshToken = storage.getItem("refresh_token");
|
|
283
|
+
let exp = 0;
|
|
284
|
+
let refreshExp = 0;
|
|
285
|
+
const claimsRaw = storage.getItem("id_token_claims_obj");
|
|
286
|
+
if (claimsRaw) {
|
|
287
|
+
try {
|
|
288
|
+
const claims = JSON.parse(claimsRaw);
|
|
289
|
+
if (claims.exp) exp = claims.exp;
|
|
290
|
+
if (claims.iat && claims.exp) {
|
|
291
|
+
const duration = claims.exp - claims.iat;
|
|
292
|
+
refreshExp = claims.iat + 3 * duration;
|
|
293
|
+
}
|
|
294
|
+
} catch (e) {
|
|
295
|
+
console.warn("\u26A0\uFE0F Could not parse id_token_claims_obj:", e);
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
exp = Number(storage.getItem("refresh_expires_in") || "0");
|
|
299
|
+
refreshExp = Number(storage.getItem("refresh_exp") || "0");
|
|
300
|
+
}
|
|
301
|
+
if (!token || !refreshToken) {
|
|
302
|
+
console.warn("\u26A0\uFE0F No tokens found in sessionStorage.");
|
|
303
|
+
return fn.apply(this, args);
|
|
304
|
+
}
|
|
305
|
+
if (now >= exp) {
|
|
306
|
+
if (now >= refreshExp) {
|
|
307
|
+
console.warn("\u26A0\uFE0F Both access and refresh tokens expired.");
|
|
308
|
+
return fn.apply(this, args);
|
|
289
309
|
}
|
|
290
310
|
try {
|
|
291
311
|
const response = yield fetch(`${accountsAPI}/user/refresh`, {
|
|
@@ -295,20 +315,20 @@ function withValidToken(fn, accountsAPI) {
|
|
|
295
315
|
});
|
|
296
316
|
if (!response.ok) throw new Error("Token refresh failed");
|
|
297
317
|
const session = yield response.json();
|
|
318
|
+
token = session.access_token;
|
|
319
|
+
refreshToken = session.refresh_token;
|
|
298
320
|
const newExp = now + session.expires_in;
|
|
299
321
|
const newRefreshExp = now + session.refresh_expires_in;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
console.error("\u{1F510} Token refresh failed:", e);
|
|
322
|
+
storage.setItem("access_token", token);
|
|
323
|
+
storage.setItem("refresh_token", refreshToken);
|
|
324
|
+
storage.setItem("refresh_expires_in", String(newExp));
|
|
325
|
+
storage.setItem("refresh_exp", String(newRefreshExp));
|
|
326
|
+
} catch (err) {
|
|
327
|
+
console.error("\u{1F510} Token refresh failed:", err);
|
|
307
328
|
throw new Error("Could not refresh session.");
|
|
308
329
|
}
|
|
309
330
|
}
|
|
310
|
-
|
|
311
|
-
return result instanceof Promise ? result : Promise.resolve(result);
|
|
331
|
+
return yield fn.apply(this, [...args, token]);
|
|
312
332
|
});
|
|
313
333
|
};
|
|
314
334
|
}
|
|
@@ -787,36 +807,37 @@ var Client = class {
|
|
|
787
807
|
this.getFolderByName = withValidToken(this.getFolderByName.bind(this), _accountURL);
|
|
788
808
|
this.getFolderByID = withValidToken(this.getFolderByID.bind(this), _accountURL);
|
|
789
809
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
// if (storage) {
|
|
793
|
-
// storage.setItem('token', session.access_token);
|
|
794
|
-
// storage.setItem('refresh_token', session.refresh_token);
|
|
795
|
-
// storage.setItem('exp', String(session.expires_in));
|
|
796
|
-
// storage.setItem('refresh_exp', String(session.refresh_expires_in));
|
|
797
|
-
// } else {
|
|
798
|
-
// console.warn("Session storage is not available.");
|
|
799
|
-
// }
|
|
800
|
-
// }
|
|
801
|
-
// public getSessionVariable(key: string): string | null {
|
|
802
|
-
// const storage = typeof window !== "undefined" ? sessionStorage : global.sessionStorage;
|
|
803
|
-
// return storage ? storage.getItem(key) : null;
|
|
804
|
-
// }
|
|
805
|
-
_setSession(session) {
|
|
810
|
+
setSession(session) {
|
|
811
|
+
const storage = typeof window !== "undefined" ? sessionStorage : global.sessionStorage;
|
|
806
812
|
const now = Math.floor(Date.now() / 1e3);
|
|
807
|
-
if (
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
813
|
+
if (storage) {
|
|
814
|
+
storage.setItem("access_token", session.access_token);
|
|
815
|
+
storage.setItem("refresh_token", session.refresh_token);
|
|
816
|
+
storage.setItem("refresh_expires_in", String(now + session.expires_in));
|
|
817
|
+
storage.setItem("refresh_exp", String(now + session.refresh_expires_in));
|
|
812
818
|
} else {
|
|
813
|
-
console.warn("
|
|
819
|
+
console.warn("Session storage is not available.");
|
|
814
820
|
}
|
|
815
821
|
}
|
|
822
|
+
getSessionVariable(key) {
|
|
823
|
+
const storage = typeof window !== "undefined" ? sessionStorage : global.sessionStorage;
|
|
824
|
+
return storage ? storage.getItem(key) : null;
|
|
825
|
+
}
|
|
826
|
+
// private _setSession(session: OidcToken): void {
|
|
827
|
+
// const now = Math.floor(Date.now() / 1000);
|
|
828
|
+
// if (typeof localStorage !== "undefined") {
|
|
829
|
+
// localStorage.setItem('token', session.access_token);
|
|
830
|
+
// localStorage.setItem('refresh_token', session.refresh_token);
|
|
831
|
+
// localStorage.setItem('exp', String(now + session.expires_in));
|
|
832
|
+
// localStorage.setItem('refresh_exp', String(now + session.refresh_expires_in));
|
|
833
|
+
// } else {
|
|
834
|
+
// console.warn("⚠️ Local Storage not available. You will need to pass tokens manually.");
|
|
835
|
+
// }
|
|
836
|
+
// }
|
|
816
837
|
login(email, password) {
|
|
817
838
|
return this._userConsumer.login(email, password).then(
|
|
818
839
|
(resp) => {
|
|
819
|
-
this.
|
|
840
|
+
this.setSession(resp);
|
|
820
841
|
return resp;
|
|
821
842
|
}
|
|
822
843
|
);
|