@opexa/portal-sdk 0.59.84 → 0.59.85

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
@@ -3,6 +3,7 @@ import { GraphQLClient, isPlainObject } from './chunk-7APXFZ5G.js';
3
3
  import './chunk-WVFSGB7Y.js';
4
4
  import { ObjectId } from '@opexa/object-id';
5
5
  export { ObjectId } from '@opexa/object-id';
6
+ import { App } from '@capacitor/app';
6
7
  import { Capacitor } from '@capacitor/core';
7
8
  import cookies from 'js-cookie';
8
9
 
@@ -335,8 +336,8 @@ function pollable(func, config) {
335
336
  };
336
337
  }
337
338
 
338
- // src/sdk/session-manager-cookie.ts
339
- var SessionManagerCookie = class {
339
+ // src/sdk/session-manager.ts
340
+ var SessionManager = class {
340
341
  logger;
341
342
  storageKey = "session";
342
343
  platformStorageKey = "session/platform";
@@ -350,10 +351,15 @@ var SessionManagerCookie = class {
350
351
  */
351
352
  v4AccessToken = null;
352
353
  v4AccessTokenExpiresAt = null;
354
+ v4RefreshPromise = null;
353
355
  constructor(config) {
354
356
  this.authService = config.authService;
355
357
  this.walletService = config.walletService;
356
358
  this.logger = config.logger;
359
+ if (config.sessionPrefix) {
360
+ this.storageKey = `${config.sessionPrefix}/${this.storageKey}`;
361
+ this.platformStorageKey = `${config.sessionPrefix}/${this.platformStorageKey}`;
362
+ }
357
363
  }
358
364
  get refreshing() {
359
365
  return this._refreshing;
@@ -364,27 +370,51 @@ var SessionManagerCookie = class {
364
370
  persist(data, version) {
365
371
  const now = /* @__PURE__ */ new Date();
366
372
  if (version === 4) {
367
- this.v4AccessToken = data.accessToken ?? null;
373
+ if (!data.session || !data.accessToken) {
374
+ this.logger.error(
375
+ "Malformed session response: expected 'session' and 'accessToken'."
376
+ );
377
+ return false;
378
+ }
379
+ this.v4AccessToken = data.accessToken;
368
380
  this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
369
- cookies.set(
381
+ localStorage.setItem(
370
382
  this.storageKey,
371
- JSON.stringify({ version, session: data.session }),
372
- { expires: subMinutes(addDays(now, 15), 2).getTime() }
383
+ JSON.stringify({ version, session: data.session })
373
384
  );
374
- return;
385
+ return true;
375
386
  }
376
- cookies.set(
387
+ localStorage.setItem(
377
388
  this.storageKey,
378
389
  JSON.stringify({
379
390
  version,
380
391
  ...data,
381
392
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
382
393
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
383
- }),
384
- { expires: subMinutes(addDays(now, 30), 2).getTime() }
394
+ })
385
395
  );
396
+ return true;
397
+ }
398
+ malformedResponseError() {
399
+ return {
400
+ ok: false,
401
+ error: {
402
+ name: "UnknownError",
403
+ message: "Something went wrong."
404
+ }
405
+ };
386
406
  }
387
407
  async create(input, version = 1) {
408
+ if (this.isServer) {
409
+ this.logger.warn("'localStorage' is not available on the server.");
410
+ return {
411
+ ok: false,
412
+ error: {
413
+ name: "UnknownError",
414
+ message: "Server sign in is not supported."
415
+ }
416
+ };
417
+ }
388
418
  if (input.type === "MAYA") {
389
419
  localStorage.setItem(this.platformStorageKey, "MAYA");
390
420
  const f0 = () => this.walletService.mayaSession({ id: input.sessionId });
@@ -410,7 +440,7 @@ var SessionManagerCookie = class {
410
440
  maxAttempt: 5
411
441
  })();
412
442
  if (!r1.ok) return r1;
413
- this.persist(r1.data, version);
443
+ if (!this.persist(r1.data, version)) return this.malformedResponseError();
414
444
  return {
415
445
  ok: true,
416
446
  data: null
@@ -419,7 +449,7 @@ var SessionManagerCookie = class {
419
449
  if (input.type === "MOBILE_NUMBER") {
420
450
  const res2 = await this.authService.createSession(input, version);
421
451
  if (res2.ok) {
422
- this.persist(res2.data, version);
452
+ if (!this.persist(res2.data, version)) return this.malformedResponseError();
423
453
  return {
424
454
  ok: true,
425
455
  data: null
@@ -431,12 +461,13 @@ var SessionManagerCookie = class {
431
461
  const res2 = await this.authService.createSession(
432
462
  {
433
463
  type: "SOCIALS",
434
- token: input.token
464
+ token: input.token,
465
+ channel: input.channel
435
466
  },
436
467
  version
437
468
  );
438
469
  if (res2.ok) {
439
- this.persist(res2.data, version);
470
+ if (!this.persist(res2.data, version)) return this.malformedResponseError();
440
471
  return {
441
472
  ok: true,
442
473
  data: null
@@ -448,7 +479,7 @@ var SessionManagerCookie = class {
448
479
  localStorage.setItem(this.platformStorageKey, "CABINET");
449
480
  const res2 = await this.authService.createSession(input, version);
450
481
  if (res2.ok) {
451
- this.persist(res2.data, version);
482
+ if (!this.persist(res2.data, version)) return this.malformedResponseError();
452
483
  return {
453
484
  ok: true,
454
485
  data: null
@@ -466,7 +497,7 @@ var SessionManagerCookie = class {
466
497
  }
467
498
  };
468
499
  }
469
- this.persist(res.data, version);
500
+ if (!this.persist(res.data, version)) return this.malformedResponseError();
470
501
  return {
471
502
  ok: true,
472
503
  data: null
@@ -476,9 +507,9 @@ var SessionManagerCookie = class {
476
507
  const res = await this.authService.authenticate(input);
477
508
  if (res.ok) {
478
509
  if (this.isServer) {
479
- this.logger.warn("'client cookies' is not available on the server.");
480
- } else {
481
- this.persist(res.data, 1);
510
+ this.logger.warn("'localStorage' is not available on the server.");
511
+ } else if (!this.persist(res.data, 1)) {
512
+ return this.malformedResponseError();
482
513
  }
483
514
  return { ok: true };
484
515
  } else {
@@ -487,7 +518,7 @@ var SessionManagerCookie = class {
487
518
  }
488
519
  async get() {
489
520
  if (this.isServer) {
490
- this.logger.warn("'client cookies' is not available on the server.");
521
+ this.logger.warn("'localStorage' is not available on the server.");
491
522
  return {
492
523
  ok: true,
493
524
  data: null
@@ -497,7 +528,7 @@ var SessionManagerCookie = class {
497
528
  await sleep(1e3);
498
529
  return await this.get();
499
530
  }
500
- const val = cookies.get(this.storageKey);
531
+ const val = localStorage.getItem(this.storageKey);
501
532
  if (!val) {
502
533
  return {
503
534
  ok: true,
@@ -515,7 +546,7 @@ var SessionManagerCookie = class {
515
546
  const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
516
547
  if (isAfter(now, refreshTokenExpiresAt)) {
517
548
  this.logger.warn("Session expired. Logging out..");
518
- cookies.remove(this.storageKey);
549
+ localStorage.removeItem(this.storageKey);
519
550
  return {
520
551
  ok: false,
521
552
  error: {
@@ -535,7 +566,7 @@ var SessionManagerCookie = class {
535
566
  if (!res.ok) {
536
567
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
537
568
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
538
- cookies.remove(this.storageKey);
569
+ localStorage.removeItem(this.storageKey);
539
570
  return {
540
571
  ok: false,
541
572
  error: res.error
@@ -557,9 +588,7 @@ var SessionManagerCookie = class {
557
588
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
558
589
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
559
590
  };
560
- cookies.set(this.storageKey, JSON.stringify(obj), {
561
- expires: subMinutes(addDays(now, 30), 2).getTime()
562
- });
591
+ localStorage.setItem(this.storageKey, JSON.stringify(obj));
563
592
  }
564
593
  return {
565
594
  ok: true,
@@ -577,13 +606,13 @@ var SessionManagerCookie = class {
577
606
  }
578
607
  async refresh() {
579
608
  if (this.isServer) {
580
- this.logger.warn("'client cookies' is not available on the server.");
609
+ this.logger.warn("'localStorage' is not available on the server.");
581
610
  return {
582
611
  ok: true,
583
612
  data: null
584
613
  };
585
614
  }
586
- const val = cookies.get(this.storageKey);
615
+ const val = localStorage.getItem(this.storageKey);
587
616
  if (!val) {
588
617
  return {
589
618
  ok: true,
@@ -607,7 +636,7 @@ var SessionManagerCookie = class {
607
636
  if (!res.ok) {
608
637
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
609
638
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
610
- cookies.remove(this.storageKey);
639
+ localStorage.removeItem(this.storageKey);
611
640
  return {
612
641
  ok: false,
613
642
  error: res.error
@@ -629,9 +658,7 @@ var SessionManagerCookie = class {
629
658
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
630
659
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
631
660
  };
632
- cookies.set(this.storageKey, JSON.stringify(obj), {
633
- expires: subMinutes(addDays(now, 30), 2).getTime()
634
- });
661
+ localStorage.setItem(this.storageKey, JSON.stringify(obj));
635
662
  return {
636
663
  ok: true,
637
664
  data: obj
@@ -661,6 +688,15 @@ var SessionManagerCookie = class {
661
688
  return await this.refreshV4(session);
662
689
  }
663
690
  async refreshV4(session) {
691
+ if (this.v4RefreshPromise) return await this.v4RefreshPromise;
692
+ this.v4RefreshPromise = this.requestV4Refresh(session);
693
+ try {
694
+ return await this.v4RefreshPromise;
695
+ } finally {
696
+ this.v4RefreshPromise = null;
697
+ }
698
+ }
699
+ async requestV4Refresh(session) {
664
700
  this.logger.info("Refreshing session...");
665
701
  this.refreshing = true;
666
702
  const res = await this.authService.refreshSession(void 0, 4);
@@ -668,7 +704,7 @@ var SessionManagerCookie = class {
668
704
  if (!res.ok) {
669
705
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
670
706
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
671
- cookies.remove(this.storageKey);
707
+ localStorage.removeItem(this.storageKey);
672
708
  this.v4AccessToken = null;
673
709
  this.v4AccessTokenExpiresAt = null;
674
710
  }
@@ -692,11 +728,11 @@ var SessionManagerCookie = class {
692
728
  }
693
729
  async destroy() {
694
730
  if (this.isServer) {
695
- this.logger.warn("'client cookies' is not available on the server.");
731
+ this.logger.warn("'localStorage' is not available on the server.");
696
732
  return;
697
733
  }
698
734
  let version = 1;
699
- const val = cookies.get(this.storageKey);
735
+ const val = localStorage.getItem(this.storageKey);
700
736
  if (val) {
701
737
  try {
702
738
  const stored = JSON.parse(val);
@@ -710,14 +746,14 @@ var SessionManagerCookie = class {
710
746
  }
711
747
  this.v4AccessToken = null;
712
748
  this.v4AccessTokenExpiresAt = null;
713
- cookies.remove(this.storageKey);
749
+ localStorage.removeItem(this.storageKey);
714
750
  }
715
751
  async verify() {
716
752
  if (this.isServer) {
717
- this.logger.warn("'client cookies' is not available on the server.");
753
+ this.logger.warn("'localStorage' is not available on the server.");
718
754
  return true;
719
755
  }
720
- const val = cookies.get(this.storageKey);
756
+ const val = localStorage.getItem(this.storageKey);
721
757
  if (val) {
722
758
  try {
723
759
  const stored = JSON.parse(val);
@@ -732,7 +768,7 @@ var SessionManagerCookie = class {
732
768
  if (!s.data) return true;
733
769
  const v = await this.authService.verifySession(s.data.accessToken);
734
770
  if (!v) {
735
- cookies.remove(this.storageKey);
771
+ localStorage.removeItem(this.storageKey);
736
772
  }
737
773
  return v;
738
774
  }
@@ -743,7 +779,7 @@ var SessionManagerCookie = class {
743
779
  }
744
780
  const v = await this.authService.verifySession(this.v4AccessToken);
745
781
  if (!v) {
746
- cookies.remove(this.storageKey);
782
+ localStorage.removeItem(this.storageKey);
747
783
  this.v4AccessToken = null;
748
784
  this.v4AccessTokenExpiresAt = null;
749
785
  }
@@ -751,7 +787,7 @@ var SessionManagerCookie = class {
751
787
  }
752
788
  get onMaya() {
753
789
  if (this.isServer) {
754
- this.logger.warn("'client cookies' is not available on the server.");
790
+ this.logger.warn("'localStorage' is not available on the server.");
755
791
  return false;
756
792
  }
757
793
  return localStorage.getItem(this.platformStorageKey) === "MAYA";
@@ -767,9 +803,7 @@ var SessionManagerCookie = class {
767
803
  return typeof window === "undefined";
768
804
  }
769
805
  };
770
-
771
- // src/sdk/session-manager.ts
772
- var SessionManager = class {
806
+ var SessionManagerCookie = class {
773
807
  logger;
774
808
  storageKey = "session";
775
809
  platformStorageKey = "session/platform";
@@ -783,15 +817,10 @@ var SessionManager = class {
783
817
  */
784
818
  v4AccessToken = null;
785
819
  v4AccessTokenExpiresAt = null;
786
- v4RefreshPromise = null;
787
820
  constructor(config) {
788
821
  this.authService = config.authService;
789
822
  this.walletService = config.walletService;
790
823
  this.logger = config.logger;
791
- if (config.sessionPrefix) {
792
- this.storageKey = `${config.sessionPrefix}/${this.storageKey}`;
793
- this.platformStorageKey = `${config.sessionPrefix}/${this.platformStorageKey}`;
794
- }
795
824
  }
796
825
  get refreshing() {
797
826
  return this._refreshing;
@@ -802,51 +831,27 @@ var SessionManager = class {
802
831
  persist(data, version) {
803
832
  const now = /* @__PURE__ */ new Date();
804
833
  if (version === 4) {
805
- if (!data.session || !data.accessToken) {
806
- this.logger.error(
807
- "Malformed session response: expected 'session' and 'accessToken'."
808
- );
809
- return false;
810
- }
811
- this.v4AccessToken = data.accessToken;
834
+ this.v4AccessToken = data.accessToken ?? null;
812
835
  this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
813
- localStorage.setItem(
836
+ cookies.set(
814
837
  this.storageKey,
815
- JSON.stringify({ version, session: data.session })
838
+ JSON.stringify({ version, session: data.session }),
839
+ { expires: subMinutes(addDays(now, 15), 2).getTime() }
816
840
  );
817
- return true;
841
+ return;
818
842
  }
819
- localStorage.setItem(
843
+ cookies.set(
820
844
  this.storageKey,
821
845
  JSON.stringify({
822
846
  version,
823
847
  ...data,
824
848
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
825
849
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
826
- })
850
+ }),
851
+ { expires: subMinutes(addDays(now, 30), 2).getTime() }
827
852
  );
828
- return true;
829
- }
830
- malformedResponseError() {
831
- return {
832
- ok: false,
833
- error: {
834
- name: "UnknownError",
835
- message: "Something went wrong."
836
- }
837
- };
838
853
  }
839
854
  async create(input, version = 1) {
840
- if (this.isServer) {
841
- this.logger.warn("'localStorage' is not available on the server.");
842
- return {
843
- ok: false,
844
- error: {
845
- name: "UnknownError",
846
- message: "Server sign in is not supported."
847
- }
848
- };
849
- }
850
855
  if (input.type === "MAYA") {
851
856
  localStorage.setItem(this.platformStorageKey, "MAYA");
852
857
  const f0 = () => this.walletService.mayaSession({ id: input.sessionId });
@@ -872,7 +877,7 @@ var SessionManager = class {
872
877
  maxAttempt: 5
873
878
  })();
874
879
  if (!r1.ok) return r1;
875
- if (!this.persist(r1.data, version)) return this.malformedResponseError();
880
+ this.persist(r1.data, version);
876
881
  return {
877
882
  ok: true,
878
883
  data: null
@@ -881,7 +886,7 @@ var SessionManager = class {
881
886
  if (input.type === "MOBILE_NUMBER") {
882
887
  const res2 = await this.authService.createSession(input, version);
883
888
  if (res2.ok) {
884
- if (!this.persist(res2.data, version)) return this.malformedResponseError();
889
+ this.persist(res2.data, version);
885
890
  return {
886
891
  ok: true,
887
892
  data: null
@@ -893,13 +898,12 @@ var SessionManager = class {
893
898
  const res2 = await this.authService.createSession(
894
899
  {
895
900
  type: "SOCIALS",
896
- token: input.token,
897
- channel: input.channel
901
+ token: input.token
898
902
  },
899
903
  version
900
904
  );
901
905
  if (res2.ok) {
902
- if (!this.persist(res2.data, version)) return this.malformedResponseError();
906
+ this.persist(res2.data, version);
903
907
  return {
904
908
  ok: true,
905
909
  data: null
@@ -911,7 +915,7 @@ var SessionManager = class {
911
915
  localStorage.setItem(this.platformStorageKey, "CABINET");
912
916
  const res2 = await this.authService.createSession(input, version);
913
917
  if (res2.ok) {
914
- if (!this.persist(res2.data, version)) return this.malformedResponseError();
918
+ this.persist(res2.data, version);
915
919
  return {
916
920
  ok: true,
917
921
  data: null
@@ -929,7 +933,7 @@ var SessionManager = class {
929
933
  }
930
934
  };
931
935
  }
932
- if (!this.persist(res.data, version)) return this.malformedResponseError();
936
+ this.persist(res.data, version);
933
937
  return {
934
938
  ok: true,
935
939
  data: null
@@ -939,9 +943,9 @@ var SessionManager = class {
939
943
  const res = await this.authService.authenticate(input);
940
944
  if (res.ok) {
941
945
  if (this.isServer) {
942
- this.logger.warn("'localStorage' is not available on the server.");
943
- } else if (!this.persist(res.data, 1)) {
944
- return this.malformedResponseError();
946
+ this.logger.warn("'client cookies' is not available on the server.");
947
+ } else {
948
+ this.persist(res.data, 1);
945
949
  }
946
950
  return { ok: true };
947
951
  } else {
@@ -950,7 +954,7 @@ var SessionManager = class {
950
954
  }
951
955
  async get() {
952
956
  if (this.isServer) {
953
- this.logger.warn("'localStorage' is not available on the server.");
957
+ this.logger.warn("'client cookies' is not available on the server.");
954
958
  return {
955
959
  ok: true,
956
960
  data: null
@@ -960,7 +964,7 @@ var SessionManager = class {
960
964
  await sleep(1e3);
961
965
  return await this.get();
962
966
  }
963
- const val = localStorage.getItem(this.storageKey);
967
+ const val = cookies.get(this.storageKey);
964
968
  if (!val) {
965
969
  return {
966
970
  ok: true,
@@ -978,7 +982,7 @@ var SessionManager = class {
978
982
  const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
979
983
  if (isAfter(now, refreshTokenExpiresAt)) {
980
984
  this.logger.warn("Session expired. Logging out..");
981
- localStorage.removeItem(this.storageKey);
985
+ cookies.remove(this.storageKey);
982
986
  return {
983
987
  ok: false,
984
988
  error: {
@@ -998,7 +1002,7 @@ var SessionManager = class {
998
1002
  if (!res.ok) {
999
1003
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
1000
1004
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
1001
- localStorage.removeItem(this.storageKey);
1005
+ cookies.remove(this.storageKey);
1002
1006
  return {
1003
1007
  ok: false,
1004
1008
  error: res.error
@@ -1020,7 +1024,9 @@ var SessionManager = class {
1020
1024
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
1021
1025
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
1022
1026
  };
1023
- localStorage.setItem(this.storageKey, JSON.stringify(obj));
1027
+ cookies.set(this.storageKey, JSON.stringify(obj), {
1028
+ expires: subMinutes(addDays(now, 30), 2).getTime()
1029
+ });
1024
1030
  }
1025
1031
  return {
1026
1032
  ok: true,
@@ -1038,13 +1044,13 @@ var SessionManager = class {
1038
1044
  }
1039
1045
  async refresh() {
1040
1046
  if (this.isServer) {
1041
- this.logger.warn("'localStorage' is not available on the server.");
1047
+ this.logger.warn("'client cookies' is not available on the server.");
1042
1048
  return {
1043
1049
  ok: true,
1044
1050
  data: null
1045
1051
  };
1046
1052
  }
1047
- const val = localStorage.getItem(this.storageKey);
1053
+ const val = cookies.get(this.storageKey);
1048
1054
  if (!val) {
1049
1055
  return {
1050
1056
  ok: true,
@@ -1068,7 +1074,7 @@ var SessionManager = class {
1068
1074
  if (!res.ok) {
1069
1075
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
1070
1076
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
1071
- localStorage.removeItem(this.storageKey);
1077
+ cookies.remove(this.storageKey);
1072
1078
  return {
1073
1079
  ok: false,
1074
1080
  error: res.error
@@ -1090,7 +1096,9 @@ var SessionManager = class {
1090
1096
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
1091
1097
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
1092
1098
  };
1093
- localStorage.setItem(this.storageKey, JSON.stringify(obj));
1099
+ cookies.set(this.storageKey, JSON.stringify(obj), {
1100
+ expires: subMinutes(addDays(now, 30), 2).getTime()
1101
+ });
1094
1102
  return {
1095
1103
  ok: true,
1096
1104
  data: obj
@@ -1120,15 +1128,6 @@ var SessionManager = class {
1120
1128
  return await this.refreshV4(session);
1121
1129
  }
1122
1130
  async refreshV4(session) {
1123
- if (this.v4RefreshPromise) return await this.v4RefreshPromise;
1124
- this.v4RefreshPromise = this.requestV4Refresh(session);
1125
- try {
1126
- return await this.v4RefreshPromise;
1127
- } finally {
1128
- this.v4RefreshPromise = null;
1129
- }
1130
- }
1131
- async requestV4Refresh(session) {
1132
1131
  this.logger.info("Refreshing session...");
1133
1132
  this.refreshing = true;
1134
1133
  const res = await this.authService.refreshSession(void 0, 4);
@@ -1136,7 +1135,7 @@ var SessionManager = class {
1136
1135
  if (!res.ok) {
1137
1136
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
1138
1137
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
1139
- localStorage.removeItem(this.storageKey);
1138
+ cookies.remove(this.storageKey);
1140
1139
  this.v4AccessToken = null;
1141
1140
  this.v4AccessTokenExpiresAt = null;
1142
1141
  }
@@ -1160,11 +1159,11 @@ var SessionManager = class {
1160
1159
  }
1161
1160
  async destroy() {
1162
1161
  if (this.isServer) {
1163
- this.logger.warn("'localStorage' is not available on the server.");
1162
+ this.logger.warn("'client cookies' is not available on the server.");
1164
1163
  return;
1165
1164
  }
1166
1165
  let version = 1;
1167
- const val = localStorage.getItem(this.storageKey);
1166
+ const val = cookies.get(this.storageKey);
1168
1167
  if (val) {
1169
1168
  try {
1170
1169
  const stored = JSON.parse(val);
@@ -1178,14 +1177,14 @@ var SessionManager = class {
1178
1177
  }
1179
1178
  this.v4AccessToken = null;
1180
1179
  this.v4AccessTokenExpiresAt = null;
1181
- localStorage.removeItem(this.storageKey);
1180
+ cookies.remove(this.storageKey);
1182
1181
  }
1183
1182
  async verify() {
1184
1183
  if (this.isServer) {
1185
- this.logger.warn("'localStorage' is not available on the server.");
1184
+ this.logger.warn("'client cookies' is not available on the server.");
1186
1185
  return true;
1187
1186
  }
1188
- const val = localStorage.getItem(this.storageKey);
1187
+ const val = cookies.get(this.storageKey);
1189
1188
  if (val) {
1190
1189
  try {
1191
1190
  const stored = JSON.parse(val);
@@ -1200,7 +1199,7 @@ var SessionManager = class {
1200
1199
  if (!s.data) return true;
1201
1200
  const v = await this.authService.verifySession(s.data.accessToken);
1202
1201
  if (!v) {
1203
- localStorage.removeItem(this.storageKey);
1202
+ cookies.remove(this.storageKey);
1204
1203
  }
1205
1204
  return v;
1206
1205
  }
@@ -1211,7 +1210,7 @@ var SessionManager = class {
1211
1210
  }
1212
1211
  const v = await this.authService.verifySession(this.v4AccessToken);
1213
1212
  if (!v) {
1214
- localStorage.removeItem(this.storageKey);
1213
+ cookies.remove(this.storageKey);
1215
1214
  this.v4AccessToken = null;
1216
1215
  this.v4AccessTokenExpiresAt = null;
1217
1216
  }
@@ -1219,7 +1218,7 @@ var SessionManager = class {
1219
1218
  }
1220
1219
  get onMaya() {
1221
1220
  if (this.isServer) {
1222
- this.logger.warn("'localStorage' is not available on the server.");
1221
+ this.logger.warn("'client cookies' is not available on the server.");
1223
1222
  return false;
1224
1223
  }
1225
1224
  return localStorage.getItem(this.platformStorageKey) === "MAYA";
@@ -3513,13 +3512,19 @@ var Sdk = class {
3513
3512
  await this.sessionManager.destroy();
3514
3513
  }
3515
3514
  watchSession(input) {
3516
- const interval = clamp(input.interval ?? 3e4, 3e4, 6e4);
3515
+ const interval = clamp(input.interval ?? 3e4, 5e3, 6e4);
3517
3516
  let timeout = null;
3517
+ let isForeground = true;
3518
+ const listener = App.addListener("appStateChange", ({ isActive }) => {
3519
+ isForeground = isActive;
3520
+ });
3518
3521
  const assignTimeout = () => {
3519
3522
  return setTimeout(async () => {
3520
- const v = await this.sessionManager.verify();
3521
- if (!v) {
3522
- await input.onInvalid();
3523
+ if (isForeground) {
3524
+ const v = await this.sessionManager.verify();
3525
+ if (!v) {
3526
+ await input.onInvalid();
3527
+ }
3523
3528
  }
3524
3529
  timeout = assignTimeout();
3525
3530
  }, interval);
@@ -3529,6 +3534,7 @@ var Sdk = class {
3529
3534
  if (timeout) {
3530
3535
  clearTimeout(timeout);
3531
3536
  }
3537
+ listener.then((handle) => handle.remove());
3532
3538
  };
3533
3539
  }
3534
3540
  async session() {