@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.cjs CHANGED
@@ -7528,6 +7528,7 @@ var SessionManager = class {
7528
7528
  v4AccessToken = null;
7529
7529
  v4AccessTokenExpiresAt = null;
7530
7530
  v4RefreshPromise = null;
7531
+ invalidReason = null;
7531
7532
  refreshTokenStore;
7532
7533
  constructor(config) {
7533
7534
  this.authService = config.authService;
@@ -7559,6 +7560,7 @@ var SessionManager = class {
7559
7560
  }
7560
7561
  this.v4AccessToken = data.accessToken;
7561
7562
  this.v4AccessTokenExpiresAt = addMinutes(now, 4).getTime();
7563
+ this.invalidReason = null;
7562
7564
  if (data.refreshToken) {
7563
7565
  await this.refreshTokenStore.set(data.refreshToken);
7564
7566
  }
@@ -7568,6 +7570,7 @@ var SessionManager = class {
7568
7570
  );
7569
7571
  return true;
7570
7572
  }
7573
+ this.invalidReason = null;
7571
7574
  localStorage.setItem(
7572
7575
  this.storageKey,
7573
7576
  JSON.stringify({
@@ -7793,6 +7796,20 @@ var SessionManager = class {
7793
7796
  };
7794
7797
  }
7795
7798
  }
7799
+ async getV4(session) {
7800
+ const now = /* @__PURE__ */ new Date();
7801
+ if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
7802
+ return {
7803
+ ok: true,
7804
+ data: {
7805
+ session,
7806
+ accessToken: this.v4AccessToken,
7807
+ accessTokenExpiresAt: this.v4AccessTokenExpiresAt
7808
+ }
7809
+ };
7810
+ }
7811
+ return await this.refreshV4(session);
7812
+ }
7796
7813
  async refresh() {
7797
7814
  if (this.isServer) {
7798
7815
  this.logger.warn("'localStorage' is not available on the server.");
@@ -7862,20 +7879,6 @@ var SessionManager = class {
7862
7879
  };
7863
7880
  }
7864
7881
  }
7865
- async getV4(session) {
7866
- const now = /* @__PURE__ */ new Date();
7867
- if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
7868
- return {
7869
- ok: true,
7870
- data: {
7871
- session,
7872
- accessToken: this.v4AccessToken,
7873
- accessTokenExpiresAt: this.v4AccessTokenExpiresAt
7874
- }
7875
- };
7876
- }
7877
- return await this.refreshV4(session);
7878
- }
7879
7882
  async refreshV4(session) {
7880
7883
  if (this.v4RefreshPromise) return await this.v4RefreshPromise;
7881
7884
  this.v4RefreshPromise = this.requestV4Refresh(session);
@@ -7894,6 +7897,8 @@ var SessionManager = class {
7894
7897
  this.logger.warn("No stored refresh token. Session expired.");
7895
7898
  this.v4AccessToken = null;
7896
7899
  this.v4AccessTokenExpiresAt = null;
7900
+ this.invalidReason = "REFRESH_TOKEN_EXPIRED";
7901
+ localStorage.removeItem(this.storageKey);
7897
7902
  return {
7898
7903
  ok: false,
7899
7904
  error: {
@@ -7905,20 +7910,25 @@ var SessionManager = class {
7905
7910
  }
7906
7911
  this.refreshing = true;
7907
7912
  const res = await this.authService.refreshSession(refreshToken, 4);
7908
- this.refreshing = false;
7909
7913
  if (!res.ok) {
7910
7914
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
7911
- if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError") {
7915
+ const terminal = res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError";
7916
+ if (terminal) {
7912
7917
  this.v4AccessToken = null;
7913
7918
  this.v4AccessTokenExpiresAt = null;
7914
- await this.refreshTokenStore.remove();
7919
+ this.invalidReason = res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION";
7920
+ localStorage.removeItem(this.storageKey);
7915
7921
  }
7922
+ this.refreshing = false;
7923
+ if (terminal) await this.refreshTokenStore.remove();
7916
7924
  return {
7917
7925
  ok: false,
7918
7926
  error: res.error
7919
7927
  };
7920
7928
  }
7929
+ this.refreshing = false;
7921
7930
  this.logger.success("Session refreshed!");
7931
+ this.invalidReason = null;
7922
7932
  if (res.data.refreshToken) {
7923
7933
  await this.refreshTokenStore.set(res.data.refreshToken);
7924
7934
  }
@@ -7954,6 +7964,7 @@ var SessionManager = class {
7954
7964
  }
7955
7965
  this.v4AccessToken = null;
7956
7966
  this.v4AccessTokenExpiresAt = null;
7967
+ this.invalidReason = null;
7957
7968
  await this.refreshTokenStore.remove();
7958
7969
  localStorage.removeItem(this.storageKey);
7959
7970
  }
@@ -7962,6 +7973,9 @@ var SessionManager = class {
7962
7973
  this.logger.warn("'localStorage' is not available on the server.");
7963
7974
  return { valid: true };
7964
7975
  }
7976
+ if (this.invalidReason) {
7977
+ return { valid: false, reason: this.invalidReason };
7978
+ }
7965
7979
  const val = localStorage.getItem(this.storageKey);
7966
7980
  if (val) {
7967
7981
  try {
@@ -7976,11 +7990,13 @@ var SessionManager = class {
7976
7990
  }
7977
7991
  const s = await this.get();
7978
7992
  if (s.error?.name === "InvalidTokenError" || s.error?.name === "SessionExpiredError" || s.error?.name === "AccountBlacklistedError") {
7993
+ this.invalidReason = "INVALID_SESSION";
7979
7994
  return { valid: false, reason: "INVALID_SESSION" };
7980
7995
  }
7981
7996
  if (!s.data) return { valid: true };
7982
7997
  const v = await this.authService.verifySession(s.data.accessToken);
7983
7998
  if (!v) {
7999
+ this.invalidReason = "INVALID_SESSION";
7984
8000
  localStorage.removeItem(this.storageKey);
7985
8001
  }
7986
8002
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -7988,16 +8004,16 @@ var SessionManager = class {
7988
8004
  async verifyV4(session) {
7989
8005
  if (!this.v4AccessToken || !this.v4AccessTokenExpiresAt || isAfter(/* @__PURE__ */ new Date(), new Date(this.v4AccessTokenExpiresAt))) {
7990
8006
  const res = await this.refreshV4(session);
7991
- return res.ok ? { valid: true } : {
7992
- valid: false,
7993
- reason: res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION"
7994
- };
8007
+ if (res.ok) return { valid: true };
8008
+ if (!this.invalidReason) return { valid: true };
8009
+ return { valid: false, reason: this.invalidReason };
7995
8010
  }
7996
8011
  const v = await this.authService.verifySession(this.v4AccessToken);
7997
8012
  if (!v) {
7998
8013
  localStorage.removeItem(this.storageKey);
7999
8014
  this.v4AccessToken = null;
8000
8015
  this.v4AccessTokenExpiresAt = null;
8016
+ this.invalidReason = "INVALID_SESSION";
8001
8017
  await this.refreshTokenStore.remove();
8002
8018
  }
8003
8019
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -8035,6 +8051,7 @@ var SessionManagerCookie = class {
8035
8051
  v4AccessToken = null;
8036
8052
  v4AccessTokenExpiresAt = null;
8037
8053
  v4RefreshPromise = null;
8054
+ invalidReason = null;
8038
8055
  refreshTokenStore;
8039
8056
  constructor(config) {
8040
8057
  this.authService = config.authService;
@@ -8050,6 +8067,7 @@ var SessionManagerCookie = class {
8050
8067
  }
8051
8068
  async persist(data, version) {
8052
8069
  const now = /* @__PURE__ */ new Date();
8070
+ this.invalidReason = null;
8053
8071
  if (version === 4) {
8054
8072
  this.v4AccessToken = data.accessToken ?? null;
8055
8073
  this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
@@ -8265,6 +8283,20 @@ var SessionManagerCookie = class {
8265
8283
  };
8266
8284
  }
8267
8285
  }
8286
+ async getV4(session) {
8287
+ const now = /* @__PURE__ */ new Date();
8288
+ if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
8289
+ return {
8290
+ ok: true,
8291
+ data: {
8292
+ session,
8293
+ accessToken: this.v4AccessToken,
8294
+ accessTokenExpiresAt: this.v4AccessTokenExpiresAt
8295
+ }
8296
+ };
8297
+ }
8298
+ return await this.refreshV4(session);
8299
+ }
8268
8300
  async refresh() {
8269
8301
  if (this.isServer) {
8270
8302
  this.logger.warn("'client cookies' is not available on the server.");
@@ -8336,20 +8368,6 @@ var SessionManagerCookie = class {
8336
8368
  };
8337
8369
  }
8338
8370
  }
8339
- async getV4(session) {
8340
- const now = /* @__PURE__ */ new Date();
8341
- if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
8342
- return {
8343
- ok: true,
8344
- data: {
8345
- session,
8346
- accessToken: this.v4AccessToken,
8347
- accessTokenExpiresAt: this.v4AccessTokenExpiresAt
8348
- }
8349
- };
8350
- }
8351
- return await this.refreshV4(session);
8352
- }
8353
8371
  async refreshV4(session) {
8354
8372
  if (this.v4RefreshPromise) return await this.v4RefreshPromise;
8355
8373
  this.v4RefreshPromise = this.requestV4Refresh(session);
@@ -8368,6 +8386,8 @@ var SessionManagerCookie = class {
8368
8386
  this.logger.warn("No stored refresh token. Session expired.");
8369
8387
  this.v4AccessToken = null;
8370
8388
  this.v4AccessTokenExpiresAt = null;
8389
+ this.invalidReason = "REFRESH_TOKEN_EXPIRED";
8390
+ cookies__default.default.remove(this.storageKey);
8371
8391
  return {
8372
8392
  ok: false,
8373
8393
  error: {
@@ -8379,20 +8399,25 @@ var SessionManagerCookie = class {
8379
8399
  }
8380
8400
  this.refreshing = true;
8381
8401
  const res = await this.authService.refreshSession(refreshToken, 4);
8382
- this.refreshing = false;
8383
8402
  if (!res.ok) {
8384
8403
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
8385
- if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError") {
8404
+ const terminal = res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError" || res.error.name === "SessionExpiredError";
8405
+ if (terminal) {
8386
8406
  this.v4AccessToken = null;
8387
8407
  this.v4AccessTokenExpiresAt = null;
8388
- await this.refreshTokenStore.remove();
8408
+ this.invalidReason = res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION";
8409
+ cookies__default.default.remove(this.storageKey);
8389
8410
  }
8411
+ this.refreshing = false;
8412
+ if (terminal) await this.refreshTokenStore.remove();
8390
8413
  return {
8391
8414
  ok: false,
8392
8415
  error: res.error
8393
8416
  };
8394
8417
  }
8418
+ this.refreshing = false;
8395
8419
  this.logger.success("Session refreshed!");
8420
+ this.invalidReason = null;
8396
8421
  if (res.data.refreshToken) {
8397
8422
  await this.refreshTokenStore.set(res.data.refreshToken);
8398
8423
  }
@@ -8428,6 +8453,7 @@ var SessionManagerCookie = class {
8428
8453
  }
8429
8454
  this.v4AccessToken = null;
8430
8455
  this.v4AccessTokenExpiresAt = null;
8456
+ this.invalidReason = null;
8431
8457
  await this.refreshTokenStore.remove();
8432
8458
  cookies__default.default.remove(this.storageKey);
8433
8459
  }
@@ -8436,6 +8462,9 @@ var SessionManagerCookie = class {
8436
8462
  this.logger.warn("'client cookies' is not available on the server.");
8437
8463
  return { valid: true };
8438
8464
  }
8465
+ if (this.invalidReason) {
8466
+ return { valid: false, reason: this.invalidReason };
8467
+ }
8439
8468
  const val = cookies__default.default.get(this.storageKey);
8440
8469
  if (val) {
8441
8470
  try {
@@ -8450,11 +8479,13 @@ var SessionManagerCookie = class {
8450
8479
  }
8451
8480
  const s = await this.get();
8452
8481
  if (s.error?.name === "InvalidTokenError" || s.error?.name === "SessionExpiredError" || s.error?.name === "AccountBlacklistedError") {
8482
+ this.invalidReason = "INVALID_SESSION";
8453
8483
  return { valid: false, reason: "INVALID_SESSION" };
8454
8484
  }
8455
8485
  if (!s.data) return { valid: true };
8456
8486
  const v = await this.authService.verifySession(s.data.accessToken);
8457
8487
  if (!v) {
8488
+ this.invalidReason = "INVALID_SESSION";
8458
8489
  cookies__default.default.remove(this.storageKey);
8459
8490
  }
8460
8491
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -8462,16 +8493,16 @@ var SessionManagerCookie = class {
8462
8493
  async verifyV4(session) {
8463
8494
  if (!this.v4AccessToken || !this.v4AccessTokenExpiresAt || isAfter(/* @__PURE__ */ new Date(), new Date(this.v4AccessTokenExpiresAt))) {
8464
8495
  const res = await this.refreshV4(session);
8465
- return res.ok ? { valid: true } : {
8466
- valid: false,
8467
- reason: res.error.name === "SessionExpiredError" ? "REFRESH_TOKEN_EXPIRED" : "INVALID_SESSION"
8468
- };
8496
+ if (res.ok) return { valid: true };
8497
+ if (!this.invalidReason) return { valid: true };
8498
+ return { valid: false, reason: this.invalidReason };
8469
8499
  }
8470
8500
  const v = await this.authService.verifySession(this.v4AccessToken);
8471
8501
  if (!v) {
8472
8502
  cookies__default.default.remove(this.storageKey);
8473
8503
  this.v4AccessToken = null;
8474
8504
  this.v4AccessTokenExpiresAt = null;
8505
+ this.invalidReason = "INVALID_SESSION";
8475
8506
  await this.refreshTokenStore.remove();
8476
8507
  }
8477
8508
  return v ? { valid: true } : { valid: false, reason: "INVALID_SESSION" };
@@ -10777,9 +10808,18 @@ var Sdk = class {
10777
10808
  let counter = 0;
10778
10809
  let timeout = null;
10779
10810
  let isForeground = true;
10811
+ let stopped = false;
10780
10812
  const listener = app.App.addListener("appStateChange", ({ isActive }) => {
10781
10813
  isForeground = isActive;
10782
10814
  });
10815
+ const stop = () => {
10816
+ stopped = true;
10817
+ if (timeout) {
10818
+ clearTimeout(timeout);
10819
+ timeout = null;
10820
+ }
10821
+ listener.then((handle) => handle.remove());
10822
+ };
10783
10823
  const assignTimeout = () => {
10784
10824
  const duration = counter <= 0 ? interval * 0.5 : interval;
10785
10825
  return setTimeout(async () => {
@@ -10788,10 +10828,13 @@ var Sdk = class {
10788
10828
  try {
10789
10829
  res = await this.sessionManager.verify();
10790
10830
  } catch {
10791
- res = { valid: false, reason: "INVALID_SESSION" };
10831
+ res = null;
10792
10832
  }
10793
- if (!res.valid) {
10833
+ if (stopped) return;
10834
+ if (res && !res.valid) {
10835
+ stop();
10794
10836
  await input.onInvalid(res.reason ?? "INVALID_SESSION");
10837
+ return;
10795
10838
  }
10796
10839
  }
10797
10840
  counter += 1;
@@ -10800,10 +10843,7 @@ var Sdk = class {
10800
10843
  };
10801
10844
  timeout = assignTimeout();
10802
10845
  return function unsubscribe() {
10803
- if (timeout) {
10804
- clearTimeout(timeout);
10805
- }
10806
- listener.then((handle) => handle.remove());
10846
+ stop();
10807
10847
  };
10808
10848
  }
10809
10849
  async session() {