@opexa/portal-sdk 0.59.83 → 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,18 +746,18 @@ 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);
724
- if (stored.version === 4) return await this.verifyV4();
760
+ if (stored.version === 4) return await this.verifyV4(stored.session);
725
761
  } catch {
726
762
  }
727
763
  }
@@ -732,17 +768,18 @@ 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
  }
739
- async verifyV4() {
775
+ async verifyV4(session) {
740
776
  if (!this.v4AccessToken || !this.v4AccessTokenExpiresAt || isAfter(/* @__PURE__ */ new Date(), new Date(this.v4AccessTokenExpiresAt))) {
741
- return true;
777
+ const res = await this.refreshV4(session);
778
+ return res.ok;
742
779
  }
743
780
  const v = await this.authService.verifySession(this.v4AccessToken);
744
781
  if (!v) {
745
- cookies.remove(this.storageKey);
782
+ localStorage.removeItem(this.storageKey);
746
783
  this.v4AccessToken = null;
747
784
  this.v4AccessTokenExpiresAt = null;
748
785
  }
@@ -750,7 +787,7 @@ var SessionManagerCookie = class {
750
787
  }
751
788
  get onMaya() {
752
789
  if (this.isServer) {
753
- this.logger.warn("'client cookies' is not available on the server.");
790
+ this.logger.warn("'localStorage' is not available on the server.");
754
791
  return false;
755
792
  }
756
793
  return localStorage.getItem(this.platformStorageKey) === "MAYA";
@@ -766,9 +803,7 @@ var SessionManagerCookie = class {
766
803
  return typeof window === "undefined";
767
804
  }
768
805
  };
769
-
770
- // src/sdk/session-manager.ts
771
- var SessionManager = class {
806
+ var SessionManagerCookie = class {
772
807
  logger;
773
808
  storageKey = "session";
774
809
  platformStorageKey = "session/platform";
@@ -782,15 +817,10 @@ var SessionManager = class {
782
817
  */
783
818
  v4AccessToken = null;
784
819
  v4AccessTokenExpiresAt = null;
785
- v4RefreshPromise = null;
786
820
  constructor(config) {
787
821
  this.authService = config.authService;
788
822
  this.walletService = config.walletService;
789
823
  this.logger = config.logger;
790
- if (config.sessionPrefix) {
791
- this.storageKey = `${config.sessionPrefix}/${this.storageKey}`;
792
- this.platformStorageKey = `${config.sessionPrefix}/${this.platformStorageKey}`;
793
- }
794
824
  }
795
825
  get refreshing() {
796
826
  return this._refreshing;
@@ -801,51 +831,27 @@ var SessionManager = class {
801
831
  persist(data, version) {
802
832
  const now = /* @__PURE__ */ new Date();
803
833
  if (version === 4) {
804
- if (!data.session || !data.accessToken) {
805
- this.logger.error(
806
- "Malformed session response: expected 'session' and 'accessToken'."
807
- );
808
- return false;
809
- }
810
- this.v4AccessToken = data.accessToken;
834
+ this.v4AccessToken = data.accessToken ?? null;
811
835
  this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
812
- localStorage.setItem(
836
+ cookies.set(
813
837
  this.storageKey,
814
- JSON.stringify({ version, session: data.session })
838
+ JSON.stringify({ version, session: data.session }),
839
+ { expires: subMinutes(addDays(now, 15), 2).getTime() }
815
840
  );
816
- return true;
841
+ return;
817
842
  }
818
- localStorage.setItem(
843
+ cookies.set(
819
844
  this.storageKey,
820
845
  JSON.stringify({
821
846
  version,
822
847
  ...data,
823
848
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
824
849
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
825
- })
850
+ }),
851
+ { expires: subMinutes(addDays(now, 30), 2).getTime() }
826
852
  );
827
- return true;
828
- }
829
- malformedResponseError() {
830
- return {
831
- ok: false,
832
- error: {
833
- name: "UnknownError",
834
- message: "Something went wrong."
835
- }
836
- };
837
853
  }
838
854
  async create(input, version = 1) {
839
- if (this.isServer) {
840
- this.logger.warn("'localStorage' is not available on the server.");
841
- return {
842
- ok: false,
843
- error: {
844
- name: "UnknownError",
845
- message: "Server sign in is not supported."
846
- }
847
- };
848
- }
849
855
  if (input.type === "MAYA") {
850
856
  localStorage.setItem(this.platformStorageKey, "MAYA");
851
857
  const f0 = () => this.walletService.mayaSession({ id: input.sessionId });
@@ -871,7 +877,7 @@ var SessionManager = class {
871
877
  maxAttempt: 5
872
878
  })();
873
879
  if (!r1.ok) return r1;
874
- if (!this.persist(r1.data, version)) return this.malformedResponseError();
880
+ this.persist(r1.data, version);
875
881
  return {
876
882
  ok: true,
877
883
  data: null
@@ -880,7 +886,7 @@ var SessionManager = class {
880
886
  if (input.type === "MOBILE_NUMBER") {
881
887
  const res2 = await this.authService.createSession(input, version);
882
888
  if (res2.ok) {
883
- if (!this.persist(res2.data, version)) return this.malformedResponseError();
889
+ this.persist(res2.data, version);
884
890
  return {
885
891
  ok: true,
886
892
  data: null
@@ -892,13 +898,12 @@ var SessionManager = class {
892
898
  const res2 = await this.authService.createSession(
893
899
  {
894
900
  type: "SOCIALS",
895
- token: input.token,
896
- channel: input.channel
901
+ token: input.token
897
902
  },
898
903
  version
899
904
  );
900
905
  if (res2.ok) {
901
- if (!this.persist(res2.data, version)) return this.malformedResponseError();
906
+ this.persist(res2.data, version);
902
907
  return {
903
908
  ok: true,
904
909
  data: null
@@ -910,7 +915,7 @@ var SessionManager = class {
910
915
  localStorage.setItem(this.platformStorageKey, "CABINET");
911
916
  const res2 = await this.authService.createSession(input, version);
912
917
  if (res2.ok) {
913
- if (!this.persist(res2.data, version)) return this.malformedResponseError();
918
+ this.persist(res2.data, version);
914
919
  return {
915
920
  ok: true,
916
921
  data: null
@@ -928,7 +933,7 @@ var SessionManager = class {
928
933
  }
929
934
  };
930
935
  }
931
- if (!this.persist(res.data, version)) return this.malformedResponseError();
936
+ this.persist(res.data, version);
932
937
  return {
933
938
  ok: true,
934
939
  data: null
@@ -938,9 +943,9 @@ var SessionManager = class {
938
943
  const res = await this.authService.authenticate(input);
939
944
  if (res.ok) {
940
945
  if (this.isServer) {
941
- this.logger.warn("'localStorage' is not available on the server.");
942
- } else if (!this.persist(res.data, 1)) {
943
- return this.malformedResponseError();
946
+ this.logger.warn("'client cookies' is not available on the server.");
947
+ } else {
948
+ this.persist(res.data, 1);
944
949
  }
945
950
  return { ok: true };
946
951
  } else {
@@ -949,7 +954,7 @@ var SessionManager = class {
949
954
  }
950
955
  async get() {
951
956
  if (this.isServer) {
952
- this.logger.warn("'localStorage' is not available on the server.");
957
+ this.logger.warn("'client cookies' is not available on the server.");
953
958
  return {
954
959
  ok: true,
955
960
  data: null
@@ -959,7 +964,7 @@ var SessionManager = class {
959
964
  await sleep(1e3);
960
965
  return await this.get();
961
966
  }
962
- const val = localStorage.getItem(this.storageKey);
967
+ const val = cookies.get(this.storageKey);
963
968
  if (!val) {
964
969
  return {
965
970
  ok: true,
@@ -977,7 +982,7 @@ var SessionManager = class {
977
982
  const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
978
983
  if (isAfter(now, refreshTokenExpiresAt)) {
979
984
  this.logger.warn("Session expired. Logging out..");
980
- localStorage.removeItem(this.storageKey);
985
+ cookies.remove(this.storageKey);
981
986
  return {
982
987
  ok: false,
983
988
  error: {
@@ -997,7 +1002,7 @@ var SessionManager = class {
997
1002
  if (!res.ok) {
998
1003
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
999
1004
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
1000
- localStorage.removeItem(this.storageKey);
1005
+ cookies.remove(this.storageKey);
1001
1006
  return {
1002
1007
  ok: false,
1003
1008
  error: res.error
@@ -1019,7 +1024,9 @@ var SessionManager = class {
1019
1024
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
1020
1025
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
1021
1026
  };
1022
- localStorage.setItem(this.storageKey, JSON.stringify(obj));
1027
+ cookies.set(this.storageKey, JSON.stringify(obj), {
1028
+ expires: subMinutes(addDays(now, 30), 2).getTime()
1029
+ });
1023
1030
  }
1024
1031
  return {
1025
1032
  ok: true,
@@ -1037,13 +1044,13 @@ var SessionManager = class {
1037
1044
  }
1038
1045
  async refresh() {
1039
1046
  if (this.isServer) {
1040
- this.logger.warn("'localStorage' is not available on the server.");
1047
+ this.logger.warn("'client cookies' is not available on the server.");
1041
1048
  return {
1042
1049
  ok: true,
1043
1050
  data: null
1044
1051
  };
1045
1052
  }
1046
- const val = localStorage.getItem(this.storageKey);
1053
+ const val = cookies.get(this.storageKey);
1047
1054
  if (!val) {
1048
1055
  return {
1049
1056
  ok: true,
@@ -1067,7 +1074,7 @@ var SessionManager = class {
1067
1074
  if (!res.ok) {
1068
1075
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
1069
1076
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
1070
- localStorage.removeItem(this.storageKey);
1077
+ cookies.remove(this.storageKey);
1071
1078
  return {
1072
1079
  ok: false,
1073
1080
  error: res.error
@@ -1089,7 +1096,9 @@ var SessionManager = class {
1089
1096
  accessTokenExpiresAt: addMinutes(now, 8).getTime(),
1090
1097
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
1091
1098
  };
1092
- localStorage.setItem(this.storageKey, JSON.stringify(obj));
1099
+ cookies.set(this.storageKey, JSON.stringify(obj), {
1100
+ expires: subMinutes(addDays(now, 30), 2).getTime()
1101
+ });
1093
1102
  return {
1094
1103
  ok: true,
1095
1104
  data: obj
@@ -1119,15 +1128,6 @@ var SessionManager = class {
1119
1128
  return await this.refreshV4(session);
1120
1129
  }
1121
1130
  async refreshV4(session) {
1122
- if (this.v4RefreshPromise) return await this.v4RefreshPromise;
1123
- this.v4RefreshPromise = this.requestV4Refresh(session);
1124
- try {
1125
- return await this.v4RefreshPromise;
1126
- } finally {
1127
- this.v4RefreshPromise = null;
1128
- }
1129
- }
1130
- async requestV4Refresh(session) {
1131
1131
  this.logger.info("Refreshing session...");
1132
1132
  this.refreshing = true;
1133
1133
  const res = await this.authService.refreshSession(void 0, 4);
@@ -1135,7 +1135,7 @@ var SessionManager = class {
1135
1135
  if (!res.ok) {
1136
1136
  this.logger.error(`Failed to refresh session: ${res.error.message}`);
1137
1137
  if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
1138
- localStorage.removeItem(this.storageKey);
1138
+ cookies.remove(this.storageKey);
1139
1139
  this.v4AccessToken = null;
1140
1140
  this.v4AccessTokenExpiresAt = null;
1141
1141
  }
@@ -1159,11 +1159,11 @@ var SessionManager = class {
1159
1159
  }
1160
1160
  async destroy() {
1161
1161
  if (this.isServer) {
1162
- this.logger.warn("'localStorage' is not available on the server.");
1162
+ this.logger.warn("'client cookies' is not available on the server.");
1163
1163
  return;
1164
1164
  }
1165
1165
  let version = 1;
1166
- const val = localStorage.getItem(this.storageKey);
1166
+ const val = cookies.get(this.storageKey);
1167
1167
  if (val) {
1168
1168
  try {
1169
1169
  const stored = JSON.parse(val);
@@ -1177,18 +1177,18 @@ var SessionManager = class {
1177
1177
  }
1178
1178
  this.v4AccessToken = null;
1179
1179
  this.v4AccessTokenExpiresAt = null;
1180
- localStorage.removeItem(this.storageKey);
1180
+ cookies.remove(this.storageKey);
1181
1181
  }
1182
1182
  async verify() {
1183
1183
  if (this.isServer) {
1184
- this.logger.warn("'localStorage' is not available on the server.");
1184
+ this.logger.warn("'client cookies' is not available on the server.");
1185
1185
  return true;
1186
1186
  }
1187
- const val = localStorage.getItem(this.storageKey);
1187
+ const val = cookies.get(this.storageKey);
1188
1188
  if (val) {
1189
1189
  try {
1190
1190
  const stored = JSON.parse(val);
1191
- if (stored.version === 4) return await this.verifyV4();
1191
+ if (stored.version === 4) return await this.verifyV4(stored.session);
1192
1192
  } catch {
1193
1193
  }
1194
1194
  }
@@ -1199,17 +1199,18 @@ var SessionManager = class {
1199
1199
  if (!s.data) return true;
1200
1200
  const v = await this.authService.verifySession(s.data.accessToken);
1201
1201
  if (!v) {
1202
- localStorage.removeItem(this.storageKey);
1202
+ cookies.remove(this.storageKey);
1203
1203
  }
1204
1204
  return v;
1205
1205
  }
1206
- async verifyV4() {
1206
+ async verifyV4(session) {
1207
1207
  if (!this.v4AccessToken || !this.v4AccessTokenExpiresAt || isAfter(/* @__PURE__ */ new Date(), new Date(this.v4AccessTokenExpiresAt))) {
1208
- return true;
1208
+ const res = await this.refreshV4(session);
1209
+ return res.ok;
1209
1210
  }
1210
1211
  const v = await this.authService.verifySession(this.v4AccessToken);
1211
1212
  if (!v) {
1212
- localStorage.removeItem(this.storageKey);
1213
+ cookies.remove(this.storageKey);
1213
1214
  this.v4AccessToken = null;
1214
1215
  this.v4AccessTokenExpiresAt = null;
1215
1216
  }
@@ -1217,7 +1218,7 @@ var SessionManager = class {
1217
1218
  }
1218
1219
  get onMaya() {
1219
1220
  if (this.isServer) {
1220
- this.logger.warn("'localStorage' is not available on the server.");
1221
+ this.logger.warn("'client cookies' is not available on the server.");
1221
1222
  return false;
1222
1223
  }
1223
1224
  return localStorage.getItem(this.platformStorageKey) === "MAYA";
@@ -3511,13 +3512,19 @@ var Sdk = class {
3511
3512
  await this.sessionManager.destroy();
3512
3513
  }
3513
3514
  watchSession(input) {
3514
- const interval = clamp(input.interval ?? 3e4, 3e4, 6e4);
3515
+ const interval = clamp(input.interval ?? 3e4, 5e3, 6e4);
3515
3516
  let timeout = null;
3517
+ let isForeground = true;
3518
+ const listener = App.addListener("appStateChange", ({ isActive }) => {
3519
+ isForeground = isActive;
3520
+ });
3516
3521
  const assignTimeout = () => {
3517
3522
  return setTimeout(async () => {
3518
- const v = await this.sessionManager.verify();
3519
- if (!v) {
3520
- await input.onInvalid();
3523
+ if (isForeground) {
3524
+ const v = await this.sessionManager.verify();
3525
+ if (!v) {
3526
+ await input.onInvalid();
3527
+ }
3521
3528
  }
3522
3529
  timeout = assignTimeout();
3523
3530
  }, interval);
@@ -3527,6 +3534,7 @@ var Sdk = class {
3527
3534
  if (timeout) {
3528
3535
  clearTimeout(timeout);
3529
3536
  }
3537
+ listener.then((handle) => handle.remove());
3530
3538
  };
3531
3539
  }
3532
3540
  async session() {