@opexa/portal-sdk 0.59.75 → 0.59.76
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/README.md +1634 -1634
- package/dist/{chunk-5WRVWQBT.js → chunk-7APXFZ5G.js} +3 -3
- package/dist/chunk-7APXFZ5G.js.map +1 -0
- package/dist/{chunk-UT6VBXJ4.js → chunk-BDZPLMTL.js} +21 -17
- package/dist/chunk-BDZPLMTL.js.map +1 -0
- package/dist/{chunk-ROBGEUSE.js → chunk-WVFSGB7Y.js} +2 -2
- package/dist/chunk-WVFSGB7Y.js.map +1 -0
- package/dist/index.cjs +224 -144
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +209 -133
- package/dist/index.js.map +1 -1
- package/dist/services/index.cjs +18 -14
- package/dist/services/index.cjs.map +1 -1
- package/dist/services/index.d.cts +4 -4
- package/dist/services/index.d.ts +4 -4
- package/dist/services/index.js +2 -2
- package/dist/{types-Dz4J_rhs.d.cts → types-kTe_j8tm.d.cts} +3 -3
- package/dist/{types-Cmawo0zW.d.ts → types-lZQpMvHU.d.ts} +3 -3
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-5WRVWQBT.js.map +0 -1
- package/dist/chunk-ROBGEUSE.js.map +0 -1
- package/dist/chunk-UT6VBXJ4.js.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { CmsPortalService, GameService, ExtensionService, FileService, WalletService, AccountService, ReportService, PortalService, TriggerService, AuthService, getFingerPrint } from './chunk-
|
|
2
|
-
import { GraphQLClient, isPlainObject } from './chunk-
|
|
3
|
-
import './chunk-
|
|
1
|
+
import { CmsPortalService, GameService, ExtensionService, FileService, WalletService, AccountService, ReportService, PortalService, TriggerService, AuthService, getFingerPrint } from './chunk-BDZPLMTL.js';
|
|
2
|
+
import { GraphQLClient, isPlainObject } from './chunk-7APXFZ5G.js';
|
|
3
|
+
import './chunk-WVFSGB7Y.js';
|
|
4
4
|
import { ObjectId } from '@opexa/object-id';
|
|
5
5
|
export { ObjectId } from '@opexa/object-id';
|
|
6
6
|
import { Capacitor } from '@capacitor/core';
|
|
@@ -343,6 +343,13 @@ var SessionManagerCookie = class {
|
|
|
343
343
|
authService;
|
|
344
344
|
walletService;
|
|
345
345
|
_refreshing = false;
|
|
346
|
+
/*
|
|
347
|
+
* v4 access tokens are never persisted (localStorage/cookie) so that an
|
|
348
|
+
* XSS-readable storage layer never holds a live access token. They only
|
|
349
|
+
* live in memory and get re-minted from the HttpOnly refresh cookie.
|
|
350
|
+
*/
|
|
351
|
+
v4AccessToken = null;
|
|
352
|
+
v4AccessTokenExpiresAt = null;
|
|
346
353
|
constructor(config) {
|
|
347
354
|
this.authService = config.authService;
|
|
348
355
|
this.walletService = config.walletService;
|
|
@@ -354,6 +361,29 @@ var SessionManagerCookie = class {
|
|
|
354
361
|
set refreshing(value) {
|
|
355
362
|
this._refreshing = value;
|
|
356
363
|
}
|
|
364
|
+
persist(data, version) {
|
|
365
|
+
const now = /* @__PURE__ */ new Date();
|
|
366
|
+
if (version === 4) {
|
|
367
|
+
this.v4AccessToken = data.accessToken ?? null;
|
|
368
|
+
this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
|
|
369
|
+
cookies.set(
|
|
370
|
+
this.storageKey,
|
|
371
|
+
JSON.stringify({ version, session: data.session }),
|
|
372
|
+
{ expires: subMinutes(addDays(now, 15), 2).getTime() }
|
|
373
|
+
);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
cookies.set(
|
|
377
|
+
this.storageKey,
|
|
378
|
+
JSON.stringify({
|
|
379
|
+
version,
|
|
380
|
+
...data,
|
|
381
|
+
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
382
|
+
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
383
|
+
}),
|
|
384
|
+
{ expires: subMinutes(addDays(now, 30), 2).getTime() }
|
|
385
|
+
);
|
|
386
|
+
}
|
|
357
387
|
async create(input, version = 1) {
|
|
358
388
|
if (input.type === "MAYA") {
|
|
359
389
|
localStorage.setItem(this.platformStorageKey, "MAYA");
|
|
@@ -380,16 +410,7 @@ var SessionManagerCookie = class {
|
|
|
380
410
|
maxAttempt: 5
|
|
381
411
|
})();
|
|
382
412
|
if (!r1.ok) return r1;
|
|
383
|
-
|
|
384
|
-
cookies.set(
|
|
385
|
-
this.storageKey,
|
|
386
|
-
JSON.stringify({
|
|
387
|
-
...r1.data,
|
|
388
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
389
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
390
|
-
}),
|
|
391
|
-
{ expires: subMinutes(addDays(now2, 30), 2).getTime() }
|
|
392
|
-
);
|
|
413
|
+
this.persist(r1.data, version);
|
|
393
414
|
return {
|
|
394
415
|
ok: true,
|
|
395
416
|
data: null
|
|
@@ -398,18 +419,7 @@ var SessionManagerCookie = class {
|
|
|
398
419
|
if (input.type === "MOBILE_NUMBER") {
|
|
399
420
|
const res2 = await this.authService.createSession(input, version);
|
|
400
421
|
if (res2.ok) {
|
|
401
|
-
|
|
402
|
-
cookies.set(
|
|
403
|
-
this.storageKey,
|
|
404
|
-
JSON.stringify({
|
|
405
|
-
...res2.data,
|
|
406
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
407
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
408
|
-
}),
|
|
409
|
-
{
|
|
410
|
-
expires: subMinutes(addDays(now2, 30), 2).getTime()
|
|
411
|
-
}
|
|
412
|
-
);
|
|
422
|
+
this.persist(res2.data, version);
|
|
413
423
|
return {
|
|
414
424
|
ok: true,
|
|
415
425
|
data: null
|
|
@@ -426,16 +436,7 @@ var SessionManagerCookie = class {
|
|
|
426
436
|
version
|
|
427
437
|
);
|
|
428
438
|
if (res2.ok) {
|
|
429
|
-
|
|
430
|
-
cookies.set(
|
|
431
|
-
this.storageKey,
|
|
432
|
-
JSON.stringify({
|
|
433
|
-
...res2.data,
|
|
434
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
435
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
436
|
-
}),
|
|
437
|
-
{ expires: subMinutes(addDays(now2, 30), 2).getTime() }
|
|
438
|
-
);
|
|
439
|
+
this.persist(res2.data, version);
|
|
439
440
|
return {
|
|
440
441
|
ok: true,
|
|
441
442
|
data: null
|
|
@@ -447,16 +448,7 @@ var SessionManagerCookie = class {
|
|
|
447
448
|
localStorage.setItem(this.platformStorageKey, "CABINET");
|
|
448
449
|
const res2 = await this.authService.createSession(input, version);
|
|
449
450
|
if (res2.ok) {
|
|
450
|
-
|
|
451
|
-
cookies.set(
|
|
452
|
-
this.storageKey,
|
|
453
|
-
JSON.stringify({
|
|
454
|
-
...res2.data,
|
|
455
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
456
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
457
|
-
}),
|
|
458
|
-
{ expires: subMinutes(addDays(now2, 30), 2).getTime() }
|
|
459
|
-
);
|
|
451
|
+
this.persist(res2.data, version);
|
|
460
452
|
return {
|
|
461
453
|
ok: true,
|
|
462
454
|
data: null
|
|
@@ -474,16 +466,7 @@ var SessionManagerCookie = class {
|
|
|
474
466
|
}
|
|
475
467
|
};
|
|
476
468
|
}
|
|
477
|
-
|
|
478
|
-
cookies.set(
|
|
479
|
-
this.storageKey,
|
|
480
|
-
JSON.stringify({
|
|
481
|
-
...res.data,
|
|
482
|
-
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
483
|
-
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
484
|
-
}),
|
|
485
|
-
{}
|
|
486
|
-
);
|
|
469
|
+
this.persist(res.data, version);
|
|
487
470
|
return {
|
|
488
471
|
ok: true,
|
|
489
472
|
data: null
|
|
@@ -492,21 +475,10 @@ var SessionManagerCookie = class {
|
|
|
492
475
|
async createFromAuthenticator(input) {
|
|
493
476
|
const res = await this.authService.authenticate(input);
|
|
494
477
|
if (res.ok) {
|
|
495
|
-
const now = /* @__PURE__ */ new Date();
|
|
496
478
|
if (this.isServer) {
|
|
497
479
|
this.logger.warn("'client cookies' is not available on the server.");
|
|
498
480
|
} else {
|
|
499
|
-
|
|
500
|
-
this.storageKey,
|
|
501
|
-
JSON.stringify({
|
|
502
|
-
...res.data,
|
|
503
|
-
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
504
|
-
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
505
|
-
}),
|
|
506
|
-
{
|
|
507
|
-
expires: subMinutes(addDays(now, 30), 2).getTime()
|
|
508
|
-
}
|
|
509
|
-
);
|
|
481
|
+
this.persist(res.data, 1);
|
|
510
482
|
}
|
|
511
483
|
return { ok: true };
|
|
512
484
|
} else {
|
|
@@ -533,7 +505,11 @@ var SessionManagerCookie = class {
|
|
|
533
505
|
};
|
|
534
506
|
}
|
|
535
507
|
try {
|
|
536
|
-
|
|
508
|
+
const stored = JSON.parse(val);
|
|
509
|
+
if (stored.version === 4) {
|
|
510
|
+
return await this.getV4(stored.session);
|
|
511
|
+
}
|
|
512
|
+
let obj = stored;
|
|
537
513
|
let now = /* @__PURE__ */ new Date();
|
|
538
514
|
const accessTokenExpiresAt = new Date(obj.accessTokenExpiresAt);
|
|
539
515
|
const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
|
|
@@ -551,7 +527,10 @@ var SessionManagerCookie = class {
|
|
|
551
527
|
if (isAfter(now, accessTokenExpiresAt)) {
|
|
552
528
|
this.logger.info("Refreshing session...");
|
|
553
529
|
this.refreshing = true;
|
|
554
|
-
const res = await this.authService.refreshSession(
|
|
530
|
+
const res = await this.authService.refreshSession(
|
|
531
|
+
obj.refreshToken,
|
|
532
|
+
obj.version ?? 1
|
|
533
|
+
);
|
|
555
534
|
this.refreshing = false;
|
|
556
535
|
if (!res.ok) {
|
|
557
536
|
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
@@ -573,7 +552,8 @@ var SessionManagerCookie = class {
|
|
|
573
552
|
now = /* @__PURE__ */ new Date();
|
|
574
553
|
obj = {
|
|
575
554
|
...obj,
|
|
576
|
-
|
|
555
|
+
accessToken: res.data.accessToken,
|
|
556
|
+
refreshToken: res.data.refreshToken ?? obj.refreshToken,
|
|
577
557
|
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
578
558
|
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
579
559
|
};
|
|
@@ -611,11 +591,18 @@ var SessionManagerCookie = class {
|
|
|
611
591
|
};
|
|
612
592
|
}
|
|
613
593
|
try {
|
|
614
|
-
|
|
594
|
+
const stored = JSON.parse(val);
|
|
595
|
+
if (stored.version === 4) {
|
|
596
|
+
return await this.refreshV4(stored.session);
|
|
597
|
+
}
|
|
598
|
+
let obj = stored;
|
|
615
599
|
let now = /* @__PURE__ */ new Date();
|
|
616
600
|
this.logger.info("Refreshing session...");
|
|
617
601
|
this.refreshing = true;
|
|
618
|
-
const res = await this.authService.refreshSession(
|
|
602
|
+
const res = await this.authService.refreshSession(
|
|
603
|
+
obj.refreshToken,
|
|
604
|
+
obj.version ?? 1
|
|
605
|
+
);
|
|
619
606
|
this.refreshing = false;
|
|
620
607
|
if (!res.ok) {
|
|
621
608
|
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
@@ -637,7 +624,8 @@ var SessionManagerCookie = class {
|
|
|
637
624
|
now = /* @__PURE__ */ new Date();
|
|
638
625
|
obj = {
|
|
639
626
|
...obj,
|
|
640
|
-
|
|
627
|
+
accessToken: res.data.accessToken,
|
|
628
|
+
refreshToken: res.data.refreshToken ?? obj.refreshToken,
|
|
641
629
|
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
642
630
|
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
643
631
|
};
|
|
@@ -658,6 +646,50 @@ var SessionManagerCookie = class {
|
|
|
658
646
|
};
|
|
659
647
|
}
|
|
660
648
|
}
|
|
649
|
+
async getV4(session) {
|
|
650
|
+
const now = /* @__PURE__ */ new Date();
|
|
651
|
+
if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
|
|
652
|
+
return {
|
|
653
|
+
ok: true,
|
|
654
|
+
data: {
|
|
655
|
+
session,
|
|
656
|
+
accessToken: this.v4AccessToken,
|
|
657
|
+
accessTokenExpiresAt: this.v4AccessTokenExpiresAt
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
return await this.refreshV4(session);
|
|
662
|
+
}
|
|
663
|
+
async refreshV4(session) {
|
|
664
|
+
this.logger.info("Refreshing session...");
|
|
665
|
+
this.refreshing = true;
|
|
666
|
+
const res = await this.authService.refreshSession(void 0, 4);
|
|
667
|
+
this.refreshing = false;
|
|
668
|
+
if (!res.ok) {
|
|
669
|
+
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
670
|
+
if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
|
|
671
|
+
cookies.remove(this.storageKey);
|
|
672
|
+
this.v4AccessToken = null;
|
|
673
|
+
this.v4AccessTokenExpiresAt = null;
|
|
674
|
+
}
|
|
675
|
+
return {
|
|
676
|
+
ok: false,
|
|
677
|
+
error: res.error
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
this.logger.success("Session refreshed!");
|
|
681
|
+
const now = /* @__PURE__ */ new Date();
|
|
682
|
+
this.v4AccessToken = res.data.accessToken;
|
|
683
|
+
this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
|
|
684
|
+
return {
|
|
685
|
+
ok: true,
|
|
686
|
+
data: {
|
|
687
|
+
session,
|
|
688
|
+
accessToken: this.v4AccessToken,
|
|
689
|
+
accessTokenExpiresAt: this.v4AccessTokenExpiresAt
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
}
|
|
661
693
|
async destroy() {
|
|
662
694
|
if (this.isServer) {
|
|
663
695
|
this.logger.warn("'client cookies' is not available on the server.");
|
|
@@ -667,6 +699,8 @@ var SessionManagerCookie = class {
|
|
|
667
699
|
if (res.data?.accessToken) {
|
|
668
700
|
await this.authService.destroySession(res.data.accessToken);
|
|
669
701
|
}
|
|
702
|
+
this.v4AccessToken = null;
|
|
703
|
+
this.v4AccessTokenExpiresAt = null;
|
|
670
704
|
cookies.remove(this.storageKey);
|
|
671
705
|
}
|
|
672
706
|
async verify() {
|
|
@@ -712,6 +746,13 @@ var SessionManager = class {
|
|
|
712
746
|
authService;
|
|
713
747
|
walletService;
|
|
714
748
|
_refreshing = false;
|
|
749
|
+
/*
|
|
750
|
+
* v4 access tokens are never persisted (localStorage/cookie) so that an
|
|
751
|
+
* XSS-readable storage layer never holds a live access token. They only
|
|
752
|
+
* live in memory and get re-minted from the HttpOnly refresh cookie.
|
|
753
|
+
*/
|
|
754
|
+
v4AccessToken = null;
|
|
755
|
+
v4AccessTokenExpiresAt = null;
|
|
715
756
|
constructor(config) {
|
|
716
757
|
this.authService = config.authService;
|
|
717
758
|
this.walletService = config.walletService;
|
|
@@ -727,6 +768,27 @@ var SessionManager = class {
|
|
|
727
768
|
set refreshing(value) {
|
|
728
769
|
this._refreshing = value;
|
|
729
770
|
}
|
|
771
|
+
persist(data, version) {
|
|
772
|
+
const now = /* @__PURE__ */ new Date();
|
|
773
|
+
if (version === 4) {
|
|
774
|
+
this.v4AccessToken = data.accessToken ?? null;
|
|
775
|
+
this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
|
|
776
|
+
localStorage.setItem(
|
|
777
|
+
this.storageKey,
|
|
778
|
+
JSON.stringify({ version, session: data.session })
|
|
779
|
+
);
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
localStorage.setItem(
|
|
783
|
+
this.storageKey,
|
|
784
|
+
JSON.stringify({
|
|
785
|
+
version,
|
|
786
|
+
...data,
|
|
787
|
+
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
788
|
+
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
789
|
+
})
|
|
790
|
+
);
|
|
791
|
+
}
|
|
730
792
|
async create(input, version = 1) {
|
|
731
793
|
if (this.isServer) {
|
|
732
794
|
this.logger.warn("'localStorage' is not available on the server.");
|
|
@@ -763,15 +825,7 @@ var SessionManager = class {
|
|
|
763
825
|
maxAttempt: 5
|
|
764
826
|
})();
|
|
765
827
|
if (!r1.ok) return r1;
|
|
766
|
-
|
|
767
|
-
localStorage.setItem(
|
|
768
|
-
this.storageKey,
|
|
769
|
-
JSON.stringify({
|
|
770
|
-
...r1.data,
|
|
771
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
772
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
773
|
-
})
|
|
774
|
-
);
|
|
828
|
+
this.persist(r1.data, version);
|
|
775
829
|
return {
|
|
776
830
|
ok: true,
|
|
777
831
|
data: null
|
|
@@ -780,15 +834,7 @@ var SessionManager = class {
|
|
|
780
834
|
if (input.type === "MOBILE_NUMBER") {
|
|
781
835
|
const res2 = await this.authService.createSession(input, version);
|
|
782
836
|
if (res2.ok) {
|
|
783
|
-
|
|
784
|
-
localStorage.setItem(
|
|
785
|
-
this.storageKey,
|
|
786
|
-
JSON.stringify({
|
|
787
|
-
...res2.data,
|
|
788
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
789
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
790
|
-
})
|
|
791
|
-
);
|
|
837
|
+
this.persist(res2.data, version);
|
|
792
838
|
return {
|
|
793
839
|
ok: true,
|
|
794
840
|
data: null
|
|
@@ -806,15 +852,7 @@ var SessionManager = class {
|
|
|
806
852
|
version
|
|
807
853
|
);
|
|
808
854
|
if (res2.ok) {
|
|
809
|
-
|
|
810
|
-
localStorage.setItem(
|
|
811
|
-
this.storageKey,
|
|
812
|
-
JSON.stringify({
|
|
813
|
-
...res2.data,
|
|
814
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
815
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
816
|
-
})
|
|
817
|
-
);
|
|
855
|
+
this.persist(res2.data, version);
|
|
818
856
|
return {
|
|
819
857
|
ok: true,
|
|
820
858
|
data: null
|
|
@@ -826,15 +864,7 @@ var SessionManager = class {
|
|
|
826
864
|
localStorage.setItem(this.platformStorageKey, "CABINET");
|
|
827
865
|
const res2 = await this.authService.createSession(input, version);
|
|
828
866
|
if (res2.ok) {
|
|
829
|
-
|
|
830
|
-
localStorage.setItem(
|
|
831
|
-
this.storageKey,
|
|
832
|
-
JSON.stringify({
|
|
833
|
-
...res2.data,
|
|
834
|
-
accessTokenExpiresAt: addMinutes(now2, 8).getTime(),
|
|
835
|
-
refreshTokenExpiresAt: subMinutes(addDays(now2, 30), 2).getTime()
|
|
836
|
-
})
|
|
837
|
-
);
|
|
867
|
+
this.persist(res2.data, version);
|
|
838
868
|
return {
|
|
839
869
|
ok: true,
|
|
840
870
|
data: null
|
|
@@ -852,15 +882,7 @@ var SessionManager = class {
|
|
|
852
882
|
}
|
|
853
883
|
};
|
|
854
884
|
}
|
|
855
|
-
|
|
856
|
-
localStorage.setItem(
|
|
857
|
-
this.storageKey,
|
|
858
|
-
JSON.stringify({
|
|
859
|
-
...res.data,
|
|
860
|
-
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
861
|
-
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
862
|
-
})
|
|
863
|
-
);
|
|
885
|
+
this.persist(res.data, version);
|
|
864
886
|
return {
|
|
865
887
|
ok: true,
|
|
866
888
|
data: null
|
|
@@ -869,18 +891,10 @@ var SessionManager = class {
|
|
|
869
891
|
async createFromAuthenticator(input) {
|
|
870
892
|
const res = await this.authService.authenticate(input);
|
|
871
893
|
if (res.ok) {
|
|
872
|
-
const now = /* @__PURE__ */ new Date();
|
|
873
894
|
if (this.isServer) {
|
|
874
895
|
this.logger.warn("'localStorage' is not available on the server.");
|
|
875
896
|
} else {
|
|
876
|
-
|
|
877
|
-
this.storageKey,
|
|
878
|
-
JSON.stringify({
|
|
879
|
-
...res.data,
|
|
880
|
-
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
881
|
-
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
882
|
-
})
|
|
883
|
-
);
|
|
897
|
+
this.persist(res.data, 1);
|
|
884
898
|
}
|
|
885
899
|
return { ok: true };
|
|
886
900
|
} else {
|
|
@@ -907,7 +921,11 @@ var SessionManager = class {
|
|
|
907
921
|
};
|
|
908
922
|
}
|
|
909
923
|
try {
|
|
910
|
-
|
|
924
|
+
const stored = JSON.parse(val);
|
|
925
|
+
if (stored.version === 4) {
|
|
926
|
+
return await this.getV4(stored.session);
|
|
927
|
+
}
|
|
928
|
+
let obj = stored;
|
|
911
929
|
let now = /* @__PURE__ */ new Date();
|
|
912
930
|
const accessTokenExpiresAt = new Date(obj.accessTokenExpiresAt);
|
|
913
931
|
const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
|
|
@@ -925,7 +943,10 @@ var SessionManager = class {
|
|
|
925
943
|
if (isAfter(now, accessTokenExpiresAt)) {
|
|
926
944
|
this.logger.info("Refreshing session...");
|
|
927
945
|
this.refreshing = true;
|
|
928
|
-
const res = await this.authService.refreshSession(
|
|
946
|
+
const res = await this.authService.refreshSession(
|
|
947
|
+
obj.refreshToken,
|
|
948
|
+
obj.version ?? 1
|
|
949
|
+
);
|
|
929
950
|
this.refreshing = false;
|
|
930
951
|
if (!res.ok) {
|
|
931
952
|
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
@@ -947,7 +968,8 @@ var SessionManager = class {
|
|
|
947
968
|
now = /* @__PURE__ */ new Date();
|
|
948
969
|
obj = {
|
|
949
970
|
...obj,
|
|
950
|
-
|
|
971
|
+
accessToken: res.data.accessToken,
|
|
972
|
+
refreshToken: res.data.refreshToken ?? obj.refreshToken,
|
|
951
973
|
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
952
974
|
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
953
975
|
};
|
|
@@ -983,11 +1005,18 @@ var SessionManager = class {
|
|
|
983
1005
|
};
|
|
984
1006
|
}
|
|
985
1007
|
try {
|
|
986
|
-
|
|
1008
|
+
const stored = JSON.parse(val);
|
|
1009
|
+
if (stored.version === 4) {
|
|
1010
|
+
return await this.refreshV4(stored.session);
|
|
1011
|
+
}
|
|
1012
|
+
let obj = stored;
|
|
987
1013
|
let now = /* @__PURE__ */ new Date();
|
|
988
1014
|
this.logger.info("Refreshing session...");
|
|
989
1015
|
this.refreshing = true;
|
|
990
|
-
const res = await this.authService.refreshSession(
|
|
1016
|
+
const res = await this.authService.refreshSession(
|
|
1017
|
+
obj.refreshToken,
|
|
1018
|
+
obj.version ?? 1
|
|
1019
|
+
);
|
|
991
1020
|
this.refreshing = false;
|
|
992
1021
|
if (!res.ok) {
|
|
993
1022
|
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
@@ -1009,7 +1038,8 @@ var SessionManager = class {
|
|
|
1009
1038
|
now = /* @__PURE__ */ new Date();
|
|
1010
1039
|
obj = {
|
|
1011
1040
|
...obj,
|
|
1012
|
-
|
|
1041
|
+
accessToken: res.data.accessToken,
|
|
1042
|
+
refreshToken: res.data.refreshToken ?? obj.refreshToken,
|
|
1013
1043
|
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
1014
1044
|
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
1015
1045
|
};
|
|
@@ -1028,6 +1058,50 @@ var SessionManager = class {
|
|
|
1028
1058
|
};
|
|
1029
1059
|
}
|
|
1030
1060
|
}
|
|
1061
|
+
async getV4(session) {
|
|
1062
|
+
const now = /* @__PURE__ */ new Date();
|
|
1063
|
+
if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
|
|
1064
|
+
return {
|
|
1065
|
+
ok: true,
|
|
1066
|
+
data: {
|
|
1067
|
+
session,
|
|
1068
|
+
accessToken: this.v4AccessToken,
|
|
1069
|
+
accessTokenExpiresAt: this.v4AccessTokenExpiresAt
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
return await this.refreshV4(session);
|
|
1074
|
+
}
|
|
1075
|
+
async refreshV4(session) {
|
|
1076
|
+
this.logger.info("Refreshing session...");
|
|
1077
|
+
this.refreshing = true;
|
|
1078
|
+
const res = await this.authService.refreshSession(void 0, 4);
|
|
1079
|
+
this.refreshing = false;
|
|
1080
|
+
if (!res.ok) {
|
|
1081
|
+
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
1082
|
+
if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
|
|
1083
|
+
localStorage.removeItem(this.storageKey);
|
|
1084
|
+
this.v4AccessToken = null;
|
|
1085
|
+
this.v4AccessTokenExpiresAt = null;
|
|
1086
|
+
}
|
|
1087
|
+
return {
|
|
1088
|
+
ok: false,
|
|
1089
|
+
error: res.error
|
|
1090
|
+
};
|
|
1091
|
+
}
|
|
1092
|
+
this.logger.success("Session refreshed!");
|
|
1093
|
+
const now = /* @__PURE__ */ new Date();
|
|
1094
|
+
this.v4AccessToken = res.data.accessToken;
|
|
1095
|
+
this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
|
|
1096
|
+
return {
|
|
1097
|
+
ok: true,
|
|
1098
|
+
data: {
|
|
1099
|
+
session,
|
|
1100
|
+
accessToken: this.v4AccessToken,
|
|
1101
|
+
accessTokenExpiresAt: this.v4AccessTokenExpiresAt
|
|
1102
|
+
}
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1031
1105
|
async destroy() {
|
|
1032
1106
|
if (this.isServer) {
|
|
1033
1107
|
this.logger.warn("'localStorage' is not available on the server.");
|
|
@@ -1037,6 +1111,8 @@ var SessionManager = class {
|
|
|
1037
1111
|
if (res.data?.accessToken) {
|
|
1038
1112
|
await this.authService.destroySession(res.data.accessToken);
|
|
1039
1113
|
}
|
|
1114
|
+
this.v4AccessToken = null;
|
|
1115
|
+
this.v4AccessTokenExpiresAt = null;
|
|
1040
1116
|
localStorage.removeItem(this.storageKey);
|
|
1041
1117
|
}
|
|
1042
1118
|
async verify() {
|