@opexa/portal-sdk 0.59.75 → 0.59.77
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-UT6VBXJ4.js → chunk-3WS4U4B7.js} +40 -24
- package/dist/chunk-3WS4U4B7.js.map +1 -0
- package/dist/{chunk-5WRVWQBT.js → chunk-7APXFZ5G.js} +3 -3
- package/dist/chunk-7APXFZ5G.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 +263 -153
- 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 +229 -135
- package/dist/index.js.map +1 -1
- package/dist/services/index.cjs +37 -21
- package/dist/services/index.cjs.map +1 -1
- package/dist/services/index.d.cts +5 -5
- package/dist/services/index.d.ts +5 -5
- 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-3WS4U4B7.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,15 +646,70 @@ 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.");
|
|
664
696
|
return;
|
|
665
697
|
}
|
|
698
|
+
let version = 1;
|
|
699
|
+
const val = cookies.get(this.storageKey);
|
|
700
|
+
if (val) {
|
|
701
|
+
try {
|
|
702
|
+
const stored = JSON.parse(val);
|
|
703
|
+
version = stored.version ?? 1;
|
|
704
|
+
} catch {
|
|
705
|
+
}
|
|
706
|
+
}
|
|
666
707
|
const res = await this.get();
|
|
667
708
|
if (res.data?.accessToken) {
|
|
668
|
-
await this.authService.destroySession(res.data.accessToken);
|
|
709
|
+
await this.authService.destroySession(res.data.accessToken, version);
|
|
669
710
|
}
|
|
711
|
+
this.v4AccessToken = null;
|
|
712
|
+
this.v4AccessTokenExpiresAt = null;
|
|
670
713
|
cookies.remove(this.storageKey);
|
|
671
714
|
}
|
|
672
715
|
async verify() {
|
|
@@ -712,6 +755,13 @@ var SessionManager = class {
|
|
|
712
755
|
authService;
|
|
713
756
|
walletService;
|
|
714
757
|
_refreshing = false;
|
|
758
|
+
/*
|
|
759
|
+
* v4 access tokens are never persisted (localStorage/cookie) so that an
|
|
760
|
+
* XSS-readable storage layer never holds a live access token. They only
|
|
761
|
+
* live in memory and get re-minted from the HttpOnly refresh cookie.
|
|
762
|
+
*/
|
|
763
|
+
v4AccessToken = null;
|
|
764
|
+
v4AccessTokenExpiresAt = null;
|
|
715
765
|
constructor(config) {
|
|
716
766
|
this.authService = config.authService;
|
|
717
767
|
this.walletService = config.walletService;
|
|
@@ -727,6 +777,27 @@ var SessionManager = class {
|
|
|
727
777
|
set refreshing(value) {
|
|
728
778
|
this._refreshing = value;
|
|
729
779
|
}
|
|
780
|
+
persist(data, version) {
|
|
781
|
+
const now = /* @__PURE__ */ new Date();
|
|
782
|
+
if (version === 4) {
|
|
783
|
+
this.v4AccessToken = data.accessToken ?? null;
|
|
784
|
+
this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
|
|
785
|
+
localStorage.setItem(
|
|
786
|
+
this.storageKey,
|
|
787
|
+
JSON.stringify({ version, session: data.session })
|
|
788
|
+
);
|
|
789
|
+
return;
|
|
790
|
+
}
|
|
791
|
+
localStorage.setItem(
|
|
792
|
+
this.storageKey,
|
|
793
|
+
JSON.stringify({
|
|
794
|
+
version,
|
|
795
|
+
...data,
|
|
796
|
+
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
797
|
+
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
798
|
+
})
|
|
799
|
+
);
|
|
800
|
+
}
|
|
730
801
|
async create(input, version = 1) {
|
|
731
802
|
if (this.isServer) {
|
|
732
803
|
this.logger.warn("'localStorage' is not available on the server.");
|
|
@@ -763,15 +834,7 @@ var SessionManager = class {
|
|
|
763
834
|
maxAttempt: 5
|
|
764
835
|
})();
|
|
765
836
|
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
|
-
);
|
|
837
|
+
this.persist(r1.data, version);
|
|
775
838
|
return {
|
|
776
839
|
ok: true,
|
|
777
840
|
data: null
|
|
@@ -780,15 +843,7 @@ var SessionManager = class {
|
|
|
780
843
|
if (input.type === "MOBILE_NUMBER") {
|
|
781
844
|
const res2 = await this.authService.createSession(input, version);
|
|
782
845
|
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
|
-
);
|
|
846
|
+
this.persist(res2.data, version);
|
|
792
847
|
return {
|
|
793
848
|
ok: true,
|
|
794
849
|
data: null
|
|
@@ -806,15 +861,7 @@ var SessionManager = class {
|
|
|
806
861
|
version
|
|
807
862
|
);
|
|
808
863
|
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
|
-
);
|
|
864
|
+
this.persist(res2.data, version);
|
|
818
865
|
return {
|
|
819
866
|
ok: true,
|
|
820
867
|
data: null
|
|
@@ -826,15 +873,7 @@ var SessionManager = class {
|
|
|
826
873
|
localStorage.setItem(this.platformStorageKey, "CABINET");
|
|
827
874
|
const res2 = await this.authService.createSession(input, version);
|
|
828
875
|
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
|
-
);
|
|
876
|
+
this.persist(res2.data, version);
|
|
838
877
|
return {
|
|
839
878
|
ok: true,
|
|
840
879
|
data: null
|
|
@@ -852,15 +891,7 @@ var SessionManager = class {
|
|
|
852
891
|
}
|
|
853
892
|
};
|
|
854
893
|
}
|
|
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
|
-
);
|
|
894
|
+
this.persist(res.data, version);
|
|
864
895
|
return {
|
|
865
896
|
ok: true,
|
|
866
897
|
data: null
|
|
@@ -869,18 +900,10 @@ var SessionManager = class {
|
|
|
869
900
|
async createFromAuthenticator(input) {
|
|
870
901
|
const res = await this.authService.authenticate(input);
|
|
871
902
|
if (res.ok) {
|
|
872
|
-
const now = /* @__PURE__ */ new Date();
|
|
873
903
|
if (this.isServer) {
|
|
874
904
|
this.logger.warn("'localStorage' is not available on the server.");
|
|
875
905
|
} 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
|
-
);
|
|
906
|
+
this.persist(res.data, 1);
|
|
884
907
|
}
|
|
885
908
|
return { ok: true };
|
|
886
909
|
} else {
|
|
@@ -907,7 +930,11 @@ var SessionManager = class {
|
|
|
907
930
|
};
|
|
908
931
|
}
|
|
909
932
|
try {
|
|
910
|
-
|
|
933
|
+
const stored = JSON.parse(val);
|
|
934
|
+
if (stored.version === 4) {
|
|
935
|
+
return await this.getV4(stored.session);
|
|
936
|
+
}
|
|
937
|
+
let obj = stored;
|
|
911
938
|
let now = /* @__PURE__ */ new Date();
|
|
912
939
|
const accessTokenExpiresAt = new Date(obj.accessTokenExpiresAt);
|
|
913
940
|
const refreshTokenExpiresAt = new Date(obj.refreshTokenExpiresAt);
|
|
@@ -925,7 +952,10 @@ var SessionManager = class {
|
|
|
925
952
|
if (isAfter(now, accessTokenExpiresAt)) {
|
|
926
953
|
this.logger.info("Refreshing session...");
|
|
927
954
|
this.refreshing = true;
|
|
928
|
-
const res = await this.authService.refreshSession(
|
|
955
|
+
const res = await this.authService.refreshSession(
|
|
956
|
+
obj.refreshToken,
|
|
957
|
+
obj.version ?? 1
|
|
958
|
+
);
|
|
929
959
|
this.refreshing = false;
|
|
930
960
|
if (!res.ok) {
|
|
931
961
|
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
@@ -947,7 +977,8 @@ var SessionManager = class {
|
|
|
947
977
|
now = /* @__PURE__ */ new Date();
|
|
948
978
|
obj = {
|
|
949
979
|
...obj,
|
|
950
|
-
|
|
980
|
+
accessToken: res.data.accessToken,
|
|
981
|
+
refreshToken: res.data.refreshToken ?? obj.refreshToken,
|
|
951
982
|
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
952
983
|
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
953
984
|
};
|
|
@@ -983,11 +1014,18 @@ var SessionManager = class {
|
|
|
983
1014
|
};
|
|
984
1015
|
}
|
|
985
1016
|
try {
|
|
986
|
-
|
|
1017
|
+
const stored = JSON.parse(val);
|
|
1018
|
+
if (stored.version === 4) {
|
|
1019
|
+
return await this.refreshV4(stored.session);
|
|
1020
|
+
}
|
|
1021
|
+
let obj = stored;
|
|
987
1022
|
let now = /* @__PURE__ */ new Date();
|
|
988
1023
|
this.logger.info("Refreshing session...");
|
|
989
1024
|
this.refreshing = true;
|
|
990
|
-
const res = await this.authService.refreshSession(
|
|
1025
|
+
const res = await this.authService.refreshSession(
|
|
1026
|
+
obj.refreshToken,
|
|
1027
|
+
obj.version ?? 1
|
|
1028
|
+
);
|
|
991
1029
|
this.refreshing = false;
|
|
992
1030
|
if (!res.ok) {
|
|
993
1031
|
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
@@ -1009,7 +1047,8 @@ var SessionManager = class {
|
|
|
1009
1047
|
now = /* @__PURE__ */ new Date();
|
|
1010
1048
|
obj = {
|
|
1011
1049
|
...obj,
|
|
1012
|
-
|
|
1050
|
+
accessToken: res.data.accessToken,
|
|
1051
|
+
refreshToken: res.data.refreshToken ?? obj.refreshToken,
|
|
1013
1052
|
accessTokenExpiresAt: addMinutes(now, 8).getTime(),
|
|
1014
1053
|
refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
|
|
1015
1054
|
};
|
|
@@ -1028,15 +1067,70 @@ var SessionManager = class {
|
|
|
1028
1067
|
};
|
|
1029
1068
|
}
|
|
1030
1069
|
}
|
|
1070
|
+
async getV4(session) {
|
|
1071
|
+
const now = /* @__PURE__ */ new Date();
|
|
1072
|
+
if (this.v4AccessToken && this.v4AccessTokenExpiresAt && !isAfter(now, new Date(this.v4AccessTokenExpiresAt))) {
|
|
1073
|
+
return {
|
|
1074
|
+
ok: true,
|
|
1075
|
+
data: {
|
|
1076
|
+
session,
|
|
1077
|
+
accessToken: this.v4AccessToken,
|
|
1078
|
+
accessTokenExpiresAt: this.v4AccessTokenExpiresAt
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
return await this.refreshV4(session);
|
|
1083
|
+
}
|
|
1084
|
+
async refreshV4(session) {
|
|
1085
|
+
this.logger.info("Refreshing session...");
|
|
1086
|
+
this.refreshing = true;
|
|
1087
|
+
const res = await this.authService.refreshSession(void 0, 4);
|
|
1088
|
+
this.refreshing = false;
|
|
1089
|
+
if (!res.ok) {
|
|
1090
|
+
this.logger.error(`Failed to refresh session: ${res.error.message}`);
|
|
1091
|
+
if (res.error.name === "InvalidTokenError" || res.error.name === "AccountBlacklistedError" || res.error.name === "VerificationLockedError") {
|
|
1092
|
+
localStorage.removeItem(this.storageKey);
|
|
1093
|
+
this.v4AccessToken = null;
|
|
1094
|
+
this.v4AccessTokenExpiresAt = null;
|
|
1095
|
+
}
|
|
1096
|
+
return {
|
|
1097
|
+
ok: false,
|
|
1098
|
+
error: res.error
|
|
1099
|
+
};
|
|
1100
|
+
}
|
|
1101
|
+
this.logger.success("Session refreshed!");
|
|
1102
|
+
const now = /* @__PURE__ */ new Date();
|
|
1103
|
+
this.v4AccessToken = res.data.accessToken;
|
|
1104
|
+
this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
|
|
1105
|
+
return {
|
|
1106
|
+
ok: true,
|
|
1107
|
+
data: {
|
|
1108
|
+
session,
|
|
1109
|
+
accessToken: this.v4AccessToken,
|
|
1110
|
+
accessTokenExpiresAt: this.v4AccessTokenExpiresAt
|
|
1111
|
+
}
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1031
1114
|
async destroy() {
|
|
1032
1115
|
if (this.isServer) {
|
|
1033
1116
|
this.logger.warn("'localStorage' is not available on the server.");
|
|
1034
1117
|
return;
|
|
1035
1118
|
}
|
|
1119
|
+
let version = 1;
|
|
1120
|
+
const val = localStorage.getItem(this.storageKey);
|
|
1121
|
+
if (val) {
|
|
1122
|
+
try {
|
|
1123
|
+
const stored = JSON.parse(val);
|
|
1124
|
+
version = stored.version ?? 1;
|
|
1125
|
+
} catch {
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1036
1128
|
const res = await this.get();
|
|
1037
1129
|
if (res.data?.accessToken) {
|
|
1038
|
-
await this.authService.destroySession(res.data.accessToken);
|
|
1130
|
+
await this.authService.destroySession(res.data.accessToken, version);
|
|
1039
1131
|
}
|
|
1132
|
+
this.v4AccessToken = null;
|
|
1133
|
+
this.v4AccessTokenExpiresAt = null;
|
|
1040
1134
|
localStorage.removeItem(this.storageKey);
|
|
1041
1135
|
}
|
|
1042
1136
|
async verify() {
|