@tiquo/dom-package 1.3.0 → 1.3.1
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.mts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +34 -2
- package/dist/index.mjs +34 -2
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -417,6 +417,7 @@ declare class TiquoAuth {
|
|
|
417
417
|
private listeners;
|
|
418
418
|
private refreshTimer;
|
|
419
419
|
private isRefreshing;
|
|
420
|
+
private visibilityHandler;
|
|
420
421
|
private broadcastChannel;
|
|
421
422
|
private tabId;
|
|
422
423
|
private isProcessingTabSync;
|
|
@@ -434,7 +435,9 @@ declare class TiquoAuth {
|
|
|
434
435
|
*/
|
|
435
436
|
getUser(): Promise<TiquoSession | null>;
|
|
436
437
|
/**
|
|
437
|
-
* Check if user is currently authenticated
|
|
438
|
+
* Check if user is currently authenticated.
|
|
439
|
+
* Returns true if the access token is valid OR a refresh token exists
|
|
440
|
+
* (meaning the session can be restored via token refresh).
|
|
438
441
|
*/
|
|
439
442
|
isAuthenticated(): boolean;
|
|
440
443
|
/**
|
|
@@ -522,6 +525,7 @@ declare class TiquoAuth {
|
|
|
522
525
|
private clearTokens;
|
|
523
526
|
private notifyListeners;
|
|
524
527
|
private scheduleRefresh;
|
|
528
|
+
private initVisibilityHandler;
|
|
525
529
|
private log;
|
|
526
530
|
private generateTabId;
|
|
527
531
|
private initTabSync;
|
package/dist/index.d.ts
CHANGED
|
@@ -417,6 +417,7 @@ declare class TiquoAuth {
|
|
|
417
417
|
private listeners;
|
|
418
418
|
private refreshTimer;
|
|
419
419
|
private isRefreshing;
|
|
420
|
+
private visibilityHandler;
|
|
420
421
|
private broadcastChannel;
|
|
421
422
|
private tabId;
|
|
422
423
|
private isProcessingTabSync;
|
|
@@ -434,7 +435,9 @@ declare class TiquoAuth {
|
|
|
434
435
|
*/
|
|
435
436
|
getUser(): Promise<TiquoSession | null>;
|
|
436
437
|
/**
|
|
437
|
-
* Check if user is currently authenticated
|
|
438
|
+
* Check if user is currently authenticated.
|
|
439
|
+
* Returns true if the access token is valid OR a refresh token exists
|
|
440
|
+
* (meaning the session can be restored via token refresh).
|
|
438
441
|
*/
|
|
439
442
|
isAuthenticated(): boolean;
|
|
440
443
|
/**
|
|
@@ -522,6 +525,7 @@ declare class TiquoAuth {
|
|
|
522
525
|
private clearTokens;
|
|
523
526
|
private notifyListeners;
|
|
524
527
|
private scheduleRefresh;
|
|
528
|
+
private initVisibilityHandler;
|
|
525
529
|
private log;
|
|
526
530
|
private generateTabId;
|
|
527
531
|
private initTabSync;
|
package/dist/index.js
CHANGED
|
@@ -684,6 +684,7 @@ var TiquoAuth = class {
|
|
|
684
684
|
this.listeners = /* @__PURE__ */ new Set();
|
|
685
685
|
this.refreshTimer = null;
|
|
686
686
|
this.isRefreshing = false;
|
|
687
|
+
this.visibilityHandler = null;
|
|
687
688
|
// Multi-tab sync
|
|
688
689
|
this.broadcastChannel = null;
|
|
689
690
|
this.isProcessingTabSync = false;
|
|
@@ -712,6 +713,7 @@ var TiquoAuth = class {
|
|
|
712
713
|
}
|
|
713
714
|
this.checkForInjectedTokens();
|
|
714
715
|
this.restoreTokens();
|
|
716
|
+
this.initVisibilityHandler();
|
|
715
717
|
}
|
|
716
718
|
// ============================================
|
|
717
719
|
// PUBLIC METHODS
|
|
@@ -788,10 +790,15 @@ var TiquoAuth = class {
|
|
|
788
790
|
return this.refreshSession();
|
|
789
791
|
}
|
|
790
792
|
/**
|
|
791
|
-
* Check if user is currently authenticated
|
|
793
|
+
* Check if user is currently authenticated.
|
|
794
|
+
* Returns true if the access token is valid OR a refresh token exists
|
|
795
|
+
* (meaning the session can be restored via token refresh).
|
|
792
796
|
*/
|
|
793
797
|
isAuthenticated() {
|
|
794
|
-
|
|
798
|
+
if (this.accessToken && !isTokenExpired(this.accessToken, 0)) {
|
|
799
|
+
return true;
|
|
800
|
+
}
|
|
801
|
+
return !!this.refreshToken;
|
|
795
802
|
}
|
|
796
803
|
/**
|
|
797
804
|
* Update the authenticated customer's profile
|
|
@@ -1176,6 +1183,13 @@ var TiquoAuth = class {
|
|
|
1176
1183
|
this.refreshToken = result.data.refresh_token;
|
|
1177
1184
|
this.saveTokens();
|
|
1178
1185
|
this.scheduleRefresh();
|
|
1186
|
+
if (this.session && this.accessToken) {
|
|
1187
|
+
const payload = decodeJWT(this.accessToken);
|
|
1188
|
+
if (payload?.exp) {
|
|
1189
|
+
this.session = { ...this.session, expiresAt: payload.exp * 1e3 };
|
|
1190
|
+
}
|
|
1191
|
+
this.notifyListeners();
|
|
1192
|
+
}
|
|
1179
1193
|
this.broadcastTabSync("TOKEN_REFRESH");
|
|
1180
1194
|
this.log("Token refreshed successfully");
|
|
1181
1195
|
return true;
|
|
@@ -1301,8 +1315,22 @@ var TiquoAuth = class {
|
|
|
1301
1315
|
this.refreshTimer = setTimeout(() => {
|
|
1302
1316
|
this.performTokenRefresh();
|
|
1303
1317
|
}, refreshIn);
|
|
1318
|
+
} else if (this.refreshToken) {
|
|
1319
|
+
this.performTokenRefresh();
|
|
1304
1320
|
}
|
|
1305
1321
|
}
|
|
1322
|
+
initVisibilityHandler() {
|
|
1323
|
+
if (typeof document === "undefined") return;
|
|
1324
|
+
this.visibilityHandler = () => {
|
|
1325
|
+
if (document.visibilityState === "visible" && this.accessToken && this.refreshToken) {
|
|
1326
|
+
if (isTokenExpired(this.accessToken, 300)) {
|
|
1327
|
+
this.log("Tab became visible with expiring token, refreshing");
|
|
1328
|
+
this.performTokenRefresh();
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
document.addEventListener("visibilitychange", this.visibilityHandler);
|
|
1333
|
+
}
|
|
1306
1334
|
log(...args) {
|
|
1307
1335
|
if (this.config.debug) {
|
|
1308
1336
|
console.log("[TiquoAuth]", ...args);
|
|
@@ -1405,6 +1433,10 @@ var TiquoAuth = class {
|
|
|
1405
1433
|
clearTimeout(this.refreshTimer);
|
|
1406
1434
|
this.refreshTimer = null;
|
|
1407
1435
|
}
|
|
1436
|
+
if (this.visibilityHandler) {
|
|
1437
|
+
document.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
1438
|
+
this.visibilityHandler = null;
|
|
1439
|
+
}
|
|
1408
1440
|
if (this.broadcastChannel) {
|
|
1409
1441
|
this.broadcastChannel.close();
|
|
1410
1442
|
this.broadcastChannel = null;
|
package/dist/index.mjs
CHANGED
|
@@ -648,6 +648,7 @@ var TiquoAuth = class {
|
|
|
648
648
|
this.listeners = /* @__PURE__ */ new Set();
|
|
649
649
|
this.refreshTimer = null;
|
|
650
650
|
this.isRefreshing = false;
|
|
651
|
+
this.visibilityHandler = null;
|
|
651
652
|
// Multi-tab sync
|
|
652
653
|
this.broadcastChannel = null;
|
|
653
654
|
this.isProcessingTabSync = false;
|
|
@@ -676,6 +677,7 @@ var TiquoAuth = class {
|
|
|
676
677
|
}
|
|
677
678
|
this.checkForInjectedTokens();
|
|
678
679
|
this.restoreTokens();
|
|
680
|
+
this.initVisibilityHandler();
|
|
679
681
|
}
|
|
680
682
|
// ============================================
|
|
681
683
|
// PUBLIC METHODS
|
|
@@ -752,10 +754,15 @@ var TiquoAuth = class {
|
|
|
752
754
|
return this.refreshSession();
|
|
753
755
|
}
|
|
754
756
|
/**
|
|
755
|
-
* Check if user is currently authenticated
|
|
757
|
+
* Check if user is currently authenticated.
|
|
758
|
+
* Returns true if the access token is valid OR a refresh token exists
|
|
759
|
+
* (meaning the session can be restored via token refresh).
|
|
756
760
|
*/
|
|
757
761
|
isAuthenticated() {
|
|
758
|
-
|
|
762
|
+
if (this.accessToken && !isTokenExpired(this.accessToken, 0)) {
|
|
763
|
+
return true;
|
|
764
|
+
}
|
|
765
|
+
return !!this.refreshToken;
|
|
759
766
|
}
|
|
760
767
|
/**
|
|
761
768
|
* Update the authenticated customer's profile
|
|
@@ -1140,6 +1147,13 @@ var TiquoAuth = class {
|
|
|
1140
1147
|
this.refreshToken = result.data.refresh_token;
|
|
1141
1148
|
this.saveTokens();
|
|
1142
1149
|
this.scheduleRefresh();
|
|
1150
|
+
if (this.session && this.accessToken) {
|
|
1151
|
+
const payload = decodeJWT(this.accessToken);
|
|
1152
|
+
if (payload?.exp) {
|
|
1153
|
+
this.session = { ...this.session, expiresAt: payload.exp * 1e3 };
|
|
1154
|
+
}
|
|
1155
|
+
this.notifyListeners();
|
|
1156
|
+
}
|
|
1143
1157
|
this.broadcastTabSync("TOKEN_REFRESH");
|
|
1144
1158
|
this.log("Token refreshed successfully");
|
|
1145
1159
|
return true;
|
|
@@ -1265,8 +1279,22 @@ var TiquoAuth = class {
|
|
|
1265
1279
|
this.refreshTimer = setTimeout(() => {
|
|
1266
1280
|
this.performTokenRefresh();
|
|
1267
1281
|
}, refreshIn);
|
|
1282
|
+
} else if (this.refreshToken) {
|
|
1283
|
+
this.performTokenRefresh();
|
|
1268
1284
|
}
|
|
1269
1285
|
}
|
|
1286
|
+
initVisibilityHandler() {
|
|
1287
|
+
if (typeof document === "undefined") return;
|
|
1288
|
+
this.visibilityHandler = () => {
|
|
1289
|
+
if (document.visibilityState === "visible" && this.accessToken && this.refreshToken) {
|
|
1290
|
+
if (isTokenExpired(this.accessToken, 300)) {
|
|
1291
|
+
this.log("Tab became visible with expiring token, refreshing");
|
|
1292
|
+
this.performTokenRefresh();
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
document.addEventListener("visibilitychange", this.visibilityHandler);
|
|
1297
|
+
}
|
|
1270
1298
|
log(...args) {
|
|
1271
1299
|
if (this.config.debug) {
|
|
1272
1300
|
console.log("[TiquoAuth]", ...args);
|
|
@@ -1369,6 +1397,10 @@ var TiquoAuth = class {
|
|
|
1369
1397
|
clearTimeout(this.refreshTimer);
|
|
1370
1398
|
this.refreshTimer = null;
|
|
1371
1399
|
}
|
|
1400
|
+
if (this.visibilityHandler) {
|
|
1401
|
+
document.removeEventListener("visibilitychange", this.visibilityHandler);
|
|
1402
|
+
this.visibilityHandler = null;
|
|
1403
|
+
}
|
|
1372
1404
|
if (this.broadcastChannel) {
|
|
1373
1405
|
this.broadcastChannel.close();
|
|
1374
1406
|
this.broadcastChannel = null;
|
package/package.json
CHANGED