@opexa/portal-sdk 0.59.96 → 0.59.97

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 CHANGED
@@ -394,6 +394,7 @@ var SessionManager = class {
394
394
  v4AccessToken = null;
395
395
  v4AccessTokenExpiresAt = null;
396
396
  v4RefreshPromise = null;
397
+ invalidReason = null;
397
398
  refreshTokenStore;
398
399
  constructor(config) {
399
400
  this.authService = config.authService;
@@ -425,6 +426,7 @@ var SessionManager = class {
425
426
  }
426
427
  this.v4AccessToken = data.accessToken;
427
428
  this.v4AccessTokenExpiresAt = addMinutes(now, 4).getTime();
429
+ this.invalidReason = null;
428
430
  if (data.refreshToken) {
429
431
  await this.refreshTokenStore.set(data.refreshToken);
430
432
  }
@@ -434,6 +436,7 @@ var SessionManager = class {
434
436
  );
435
437
  return true;
436
438
  }
439
+ this.invalidReason = null;
437
440
  localStorage.setItem(
438
441
  this.storageKey,
439
442
  JSON.stringify({
@@ -659,6 +662,20 @@ var SessionManager = class {
659
662
  };
660
663
  }
661
664
  }
665
+ async getV4(session) {
666
+ const now = /* @__PURE__ */ new Date();
667
+ if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
668
+ return {
669
+ ok: true,
670
+ data: {
671
+ session,
672
+ accessToken: this.v4AccessToken,
673
+ accessTokenExpiresAt: this.v4AccessTokenExpiresAt
674
+ }
675
+ };
676
+ }
677
+ return await this.refreshV4(session);
678
+ }
662
679
  async refresh() {
663
680
  if (this.isServer) {
664
681
  this.logger.warn("'localStorage' is not available on the server.");
@@ -728,20 +745,6 @@ var SessionManager = class {
728
745
  };
729
746
  }
730
747
  }
731
- async getV4(session) {
732
- const now = /* @__PURE__ */ new Date();
733
- if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
734
- return {
735
- ok: true,
736
- data: {
737
- session,
738
- accessToken: this.v4AccessToken,
739
- accessTokenExpiresAt: this.v4AccessTokenExpiresAt
740
- }
741
- };
742
- }
743
- return await this.refreshV4(session);
744
- }
745
748
  async refreshV4(session) {
746
749
  if (this.v4RefreshPromise) return await this.v4RefreshPromise;
747
750
  this.v4RefreshPromise = this.requestV4Refresh(session);
@@ -760,6 +763,8 @@ var SessionManager = class {
760
763
  this.logger.warn("No stored refresh token. Session expired.");
761
764
  this.v4AccessToken = null;
762
765
  this.v4AccessTokenExpiresAt = null;
766
+ this.invalidReason = "REFRESH_TOKEN_EXPIRED";
767
+ localStorage.removeItem(this.storageKey);
763
768
  return {
764
769
  ok: false,
765
770
  error: {
@@ -771,20 +776,25 @@ var SessionManager = class {
771
776
  }
772
777
  this.refreshing = true;
773
778
  const res = await this.authService.refreshSession(refreshToken, 4);
774
- this.refreshing = false;
775
779
  if (!res.ok) {
776
780
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
777
- if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError") {
781
+ const terminal = res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError";
782
+ if (terminal) {
778
783
  this.v4AccessToken = null;
779
784
  this.v4AccessTokenExpiresAt = null;
780
- await this.refreshTokenStore.remove();
785
+ this.invalidReason = res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION";
786
+ localStorage.removeItem(this.storageKey);
781
787
  }
788
+ this.refreshing = false;
789
+ if (terminal) await this.refreshTokenStore.remove();
782
790
  return {
783
791
  ok: false,
784
792
  error: res.error
785
793
  };
786
794
  }
795
+ this.refreshing = false;
787
796
  this.logger.success("Session refreshed!");
797
+ this.invalidReason = null;
788
798
  if (res.data.refreshToken) {
789
799
  await this.refreshTokenStore.set(res.data.refreshToken);
790
800
  }
@@ -820,6 +830,7 @@ var SessionManager = class {
820
830
  }
821
831
  this.v4AccessToken = null;
822
832
  this.v4AccessTokenExpiresAt = null;
833
+ this.invalidReason = null;
823
834
  await this.refreshTokenStore.remove();
824
835
  localStorage.removeItem(this.storageKey);
825
836
  }
@@ -828,6 +839,9 @@ var SessionManager = class {
828
839
  this.logger.warn("'localStorage' is not available on the server.");
829
840
  return { valid: true };
830
841
  }
842
+ if (this.invalidReason) {
843
+ return { valid: false, reason: this.invalidReason };
844
+ }
831
845
  const val = localStorage.getItem(this.storageKey);
832
846
  if (val) {
833
847
  try {
@@ -842,11 +856,13 @@ var SessionManager = class {
842
856
  }
843
857
  const s = await this.get();
844
858
  if (s.error?.name === "InvalidTokenError" || s.error?.name === "SessionExpiredError" || s.error?.name === "AccountBlacklistedError") {
859
+ this.invalidReason = "INVALID_SESSION";
845
860
  return { valid: false, reason: "INVALID_SESSION" };
846
861
  }
847
862
  if (!s.data) return { valid: true };
848
863
  const v = await this.authService.verifySession(s.data.accessToken);
849
864
  if (!v) {
865
+ this.invalidReason = "INVALID_SESSION";
850
866
  localStorage.removeItem(this.storageKey);
851
867
  }
852
868
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -854,16 +870,16 @@ var SessionManager = class {
854
870
  async verifyV4(session) {
855
871
  if (!this.v4AccessToken || !this.v4AccessTokenExpiresAt || isAfter(/* @__PURE__ */ new Date(), new Date(this.v4AccessTokenExpiresAt))) {
856
872
  const res = await this.refreshV4(session);
857
- return res.ok ? { valid: true } : {
858
- valid: false,
859
- reason: res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION"
860
- };
873
+ if (res.ok) return { valid: true };
874
+ if (!this.invalidReason) return { valid: true };
875
+ return { valid: false, reason: this.invalidReason };
861
876
  }
862
877
  const v = await this.authService.verifySession(this.v4AccessToken);
863
878
  if (!v) {
864
879
  localStorage.removeItem(this.storageKey);
865
880
  this.v4AccessToken = null;
866
881
  this.v4AccessTokenExpiresAt = null;
882
+ this.invalidReason = "INVALID_SESSION";
867
883
  await this.refreshTokenStore.remove();
868
884
  }
869
885
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -901,6 +917,7 @@ var SessionManagerCookie = class {
901
917
  v4AccessToken = null;
902
918
  v4AccessTokenExpiresAt = null;
903
919
  v4RefreshPromise = null;
920
+ invalidReason = null;
904
921
  refreshTokenStore;
905
922
  constructor(config) {
906
923
  this.authService = config.authService;
@@ -916,6 +933,7 @@ var SessionManagerCookie = class {
916
933
  }
917
934
  async persist(data, version) {
918
935
  const now = /* @__PURE__ */ new Date();
936
+ this.invalidReason = null;
919
937
  if (version === 4) {
920
938
  this.v4AccessToken = data.accessToken ?? null;
921
939
  this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
@@ -1131,6 +1149,20 @@ var SessionManagerCookie = class {
1131
1149
  };
1132
1150
  }
1133
1151
  }
1152
+ async getV4(session) {
1153
+ const now = /* @__PURE__ */ new Date();
1154
+ if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
1155
+ return {
1156
+ ok: true,
1157
+ data: {
1158
+ session,
1159
+ accessToken: this.v4AccessToken,
1160
+ accessTokenExpiresAt: this.v4AccessTokenExpiresAt
1161
+ }
1162
+ };
1163
+ }
1164
+ return await this.refreshV4(session);
1165
+ }
1134
1166
  async refresh() {
1135
1167
  if (this.isServer) {
1136
1168
  this.logger.warn("'client cookies' is not available on the server.");
@@ -1202,20 +1234,6 @@ var SessionManagerCookie = class {
1202
1234
  };
1203
1235
  }
1204
1236
  }
1205
- async getV4(session) {
1206
- const now = /* @__PURE__ */ new Date();
1207
- if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
1208
- return {
1209
- ok: true,
1210
- data: {
1211
- session,
1212
- accessToken: this.v4AccessToken,
1213
- accessTokenExpiresAt: this.v4AccessTokenExpiresAt
1214
- }
1215
- };
1216
- }
1217
- return await this.refreshV4(session);
1218
- }
1219
1237
  async refreshV4(session) {
1220
1238
  if (this.v4RefreshPromise) return await this.v4RefreshPromise;
1221
1239
  this.v4RefreshPromise = this.requestV4Refresh(session);
@@ -1234,6 +1252,8 @@ var SessionManagerCookie = class {
1234
1252
  this.logger.warn("No stored refresh token. Session expired.");
1235
1253
  this.v4AccessToken = null;
1236
1254
  this.v4AccessTokenExpiresAt = null;
1255
+ this.invalidReason = "REFRESH_TOKEN_EXPIRED";
1256
+ cookies.remove(this.storageKey);
1237
1257
  return {
1238
1258
  ok: false,
1239
1259
  error: {
@@ -1245,20 +1265,25 @@ var SessionManagerCookie = class {
1245
1265
  }
1246
1266
  this.refreshing = true;
1247
1267
  const res = await this.authService.refreshSession(refreshToken, 4);
1248
- this.refreshing = false;
1249
1268
  if (!res.ok) {
1250
1269
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
1251
- if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError") {
1270
+ const terminal = res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError";
1271
+ if (terminal) {
1252
1272
  this.v4AccessToken = null;
1253
1273
  this.v4AccessTokenExpiresAt = null;
1254
- await this.refreshTokenStore.remove();
1274
+ this.invalidReason = res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION";
1275
+ cookies.remove(this.storageKey);
1255
1276
  }
1277
+ this.refreshing = false;
1278
+ if (terminal) await this.refreshTokenStore.remove();
1256
1279
  return {
1257
1280
  ok: false,
1258
1281
  error: res.error
1259
1282
  };
1260
1283
  }
1284
+ this.refreshing = false;
1261
1285
  this.logger.success("Session refreshed!");
1286
+ this.invalidReason = null;
1262
1287
  if (res.data.refreshToken) {
1263
1288
  await this.refreshTokenStore.set(res.data.refreshToken);
1264
1289
  }
@@ -1294,6 +1319,7 @@ var SessionManagerCookie = class {
1294
1319
  }
1295
1320
  this.v4AccessToken = null;
1296
1321
  this.v4AccessTokenExpiresAt = null;
1322
+ this.invalidReason = null;
1297
1323
  await this.refreshTokenStore.remove();
1298
1324
  cookies.remove(this.storageKey);
1299
1325
  }
@@ -1302,6 +1328,9 @@ var SessionManagerCookie = class {
1302
1328
  this.logger.warn("'client cookies' is not available on the server.");
1303
1329
  return { valid: true };
1304
1330
  }
1331
+ if (this.invalidReason) {
1332
+ return { valid: false, reason: this.invalidReason };
1333
+ }
1305
1334
  const val = cookies.get(this.storageKey);
1306
1335
  if (val) {
1307
1336
  try {
@@ -1316,11 +1345,13 @@ var SessionManagerCookie = class {
1316
1345
  }
1317
1346
  const s = await this.get();
1318
1347
  if (s.error?.name === "InvalidTokenError" || s.error?.name === "SessionExpiredError" || s.error?.name === "AccountBlacklistedError") {
1348
+ this.invalidReason = "INVALID_SESSION";
1319
1349
  return { valid: false, reason: "INVALID_SESSION" };
1320
1350
  }
1321
1351
  if (!s.data) return { valid: true };
1322
1352
  const v = await this.authService.verifySession(s.data.accessToken);
1323
1353
  if (!v) {
1354
+ this.invalidReason = "INVALID_SESSION";
1324
1355
  cookies.remove(this.storageKey);
1325
1356
  }
1326
1357
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -1328,16 +1359,16 @@ var SessionManagerCookie = class {
1328
1359
  async verifyV4(session) {
1329
1360
  if (!this.v4AccessToken || !this.v4AccessTokenExpiresAt || isAfter(/* @__PURE__ */ new Date(), new Date(this.v4AccessTokenExpiresAt))) {
1330
1361
  const res = await this.refreshV4(session);
1331
- return res.ok ? { valid: true } : {
1332
- valid: false,
1333
- reason: res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION"
1334
- };
1362
+ if (res.ok) return { valid: true };
1363
+ if (!this.invalidReason) return { valid: true };
1364
+ return { valid: false, reason: this.invalidReason };
1335
1365
  }
1336
1366
  const v = await this.authService.verifySession(this.v4AccessToken);
1337
1367
  if (!v) {
1338
1368
  cookies.remove(this.storageKey);
1339
1369
  this.v4AccessToken = null;
1340
1370
  this.v4AccessTokenExpiresAt = null;
1371
+ this.invalidReason = "INVALID_SESSION";
1341
1372
  await this.refreshTokenStore.remove();
1342
1373
  }
1343
1374
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -3643,9 +3674,18 @@ var Sdk = class {
3643
3674
  let counter = 0;
3644
3675
  let timeout = null;
3645
3676
  let isForeground = true;
3677
+ let stopped = false;
3646
3678
  const listener = App.addListener("appStateChange", ({ isActive }) => {
3647
3679
  isForeground = isActive;
3648
3680
  });
3681
+ const stop = () => {
3682
+ stopped = true;
3683
+ if (timeout) {
3684
+ clearTimeout(timeout);
3685
+ timeout = null;
3686
+ }
3687
+ listener.then((handle) => handle.remove());
3688
+ };
3649
3689
  const assignTimeout = () => {
3650
3690
  const duration = counter <= 0 ? interval * 0.5 : interval;
3651
3691
  return setTimeout(async () => {
@@ -3654,10 +3694,13 @@ var Sdk = class {
3654
3694
  try {
3655
3695
  res = await this.sessionManager.verify();
3656
3696
  } catch {
3657
- res = { valid: false, reason: "INVALID_SESSION" };
3697
+ res = null;
3658
3698
  }
3659
- if (!res.valid) {
3699
+ if (stopped) return;
3700
+ if (res && !res.valid) {
3701
+ stop();
3660
3702
  await input.onInvalid(res.reason ?? "INVALID_SESSION");
3703
+ return;
3661
3704
  }
3662
3705
  }
3663
3706
  counter += 1;
@@ -3666,10 +3709,7 @@ var Sdk = class {
3666
3709
  };
3667
3710
  timeout = assignTimeout();
3668
3711
  return function unsubscribe() {
3669
- if (timeout) {
3670
- clearTimeout(timeout);
3671
- }
3672
- listener.then((handle) => handle.remove());
3712
+ stop();
3673
3713
  };
3674
3714
  }
3675
3715
  async session() {