@opexa/portal-sdk 0.59.76 → 0.59.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -4500,7 +4500,7 @@ var AuthService = class {
4500
4500
  }
4501
4501
  async createSession(input, version = 1) {
4502
4502
  const headers = new Headers(this.headers);
4503
- if (version === 3) {
4503
+ if (version === 3 || version === 4) {
4504
4504
  const fingerprint = await getFingerPrint();
4505
4505
  if (fingerprint) {
4506
4506
  headers.append("Fingerprint", fingerprint);
@@ -4544,26 +4544,26 @@ var AuthService = class {
4544
4544
  body: JSON.stringify(input),
4545
4545
  ...version === 4 && { credentials: "include" }
4546
4546
  });
4547
- const data = await res.json();
4547
+ const body = await res.json();
4548
4548
  if (res.ok) {
4549
4549
  return {
4550
4550
  ok: true,
4551
- data
4551
+ data: version !== 1 ? body.data ?? body : body
4552
4552
  };
4553
4553
  }
4554
- if (data.code === "ACCOUNT_BLACKLISTED") {
4554
+ if (body.code === "ACCOUNT_BLACKLISTED") {
4555
4555
  return {
4556
4556
  ok: false,
4557
4557
  error: createOperationError("AccountBlacklistedError")
4558
4558
  };
4559
4559
  }
4560
- if (data.code === "MEMBER_ACCOUNT_SUSPENDED") {
4560
+ if (body.code === "MEMBER_ACCOUNT_SUSPENDED") {
4561
4561
  return {
4562
4562
  ok: false,
4563
4563
  error: createOperationError("AccountSuspendedError")
4564
4564
  };
4565
4565
  }
4566
- if (data.code === "INVALID_RECAPTCHA_RESPONSE") {
4566
+ if (body.code === "INVALID_RECAPTCHA_RESPONSE") {
4567
4567
  return {
4568
4568
  ok: false,
4569
4569
  error: createOperationError("InvalidReCAPTCHAResponseError")
@@ -4629,29 +4629,35 @@ var AuthService = class {
4629
4629
  if (version !== 4) {
4630
4630
  headers.append("Authorization", `Bearer ${refreshToken}`);
4631
4631
  }
4632
+ if (version === 3 || version === 4) {
4633
+ const fingerprint = await getFingerPrint();
4634
+ if (fingerprint) {
4635
+ headers.append("Fingerprint", fingerprint);
4636
+ }
4637
+ }
4632
4638
  try {
4633
4639
  const res = await fetch(
4634
- `${this.url}${version === 4 ? "/v4/session/refresh" : "/session:refresh"}`,
4640
+ `${this.url}${version !== 1 ? `/v${version}/session/refresh` : "/session:refresh"}`,
4635
4641
  {
4636
4642
  method: "POST",
4637
4643
  headers,
4638
4644
  ...version === 4 && { credentials: "include" }
4639
4645
  }
4640
4646
  );
4641
- const data = await res.json();
4647
+ const body = await res.json();
4642
4648
  if (res.ok) {
4643
4649
  return {
4644
4650
  ok: true,
4645
- data
4651
+ data: version !== 1 ? body.data ?? body : body
4646
4652
  };
4647
4653
  }
4648
- if (data.code === "ACCOUNT_BLACKLISTED") {
4654
+ if (body.code === "ACCOUNT_BLACKLISTED") {
4649
4655
  return {
4650
4656
  ok: false,
4651
4657
  error: createOperationError("AccountBlacklistedError")
4652
4658
  };
4653
4659
  }
4654
- if (data.code === "MEMBER_ACCOUNT_SUSPENDED") {
4660
+ if (body.code === "MEMBER_ACCOUNT_SUSPENDED") {
4655
4661
  return {
4656
4662
  ok: false,
4657
4663
  error: createOperationError("AccountSuspendedError")
@@ -4674,14 +4680,18 @@ var AuthService = class {
4674
4680
  };
4675
4681
  }
4676
4682
  }
4677
- async destroySession(accessToken) {
4683
+ async destroySession(accessToken, version = 1) {
4678
4684
  const headers = new Headers(this.headers);
4679
4685
  headers.append("Authorization", `Bearer ${accessToken}`);
4680
4686
  try {
4681
- const res = await fetch(`${this.url}/session`, {
4682
- method: "DELETE",
4683
- headers
4684
- });
4687
+ const res = await fetch(
4688
+ `${this.url}${version !== 1 ? `/v${version}/session` : "/session"}`,
4689
+ {
4690
+ method: "DELETE",
4691
+ headers,
4692
+ ...version === 4 && { credentials: "include" }
4693
+ }
4694
+ );
4685
4695
  return res.ok ? { ok: true } : { ok: false, error: statusCodeToOperationError(res.status) };
4686
4696
  } catch {
4687
4697
  return { ok: false, error: statusCodeToOperationError(500) };
@@ -7459,9 +7469,18 @@ var SessionManagerCookie = class {
7459
7469
  this.logger.warn("'client cookies' is not available on the server.");
7460
7470
  return;
7461
7471
  }
7472
+ let version = 1;
7473
+ const val = cookies__default.default.get(this.storageKey);
7474
+ if (val) {
7475
+ try {
7476
+ const stored = JSON.parse(val);
7477
+ version = stored.version ?? 1;
7478
+ } catch {
7479
+ }
7480
+ }
7462
7481
  const res = await this.get();
7463
7482
  if (res.data?.accessToken) {
7464
- await this.authService.destroySession(res.data.accessToken);
7483
+ await this.authService.destroySession(res.data.accessToken, version);
7465
7484
  }
7466
7485
  this.v4AccessToken = null;
7467
7486
  this.v4AccessTokenExpiresAt = null;
@@ -7517,6 +7536,7 @@ var SessionManager = class {
7517
7536
  */
7518
7537
  v4AccessToken = null;
7519
7538
  v4AccessTokenExpiresAt = null;
7539
+ v4RefreshPromise = null;
7520
7540
  constructor(config) {
7521
7541
  this.authService = config.authService;
7522
7542
  this.walletService = config.walletService;
@@ -7535,13 +7555,19 @@ var SessionManager = class {
7535
7555
  persist(data, version) {
7536
7556
  const now = /* @__PURE__ */ new Date();
7537
7557
  if (version === 4) {
7538
- this.v4AccessToken = data.accessToken ?? null;
7558
+ if (!data.session || !data.accessToken) {
7559
+ this.logger.error(
7560
+ "Malformed session response: expected 'session' and 'accessToken'."
7561
+ );
7562
+ return false;
7563
+ }
7564
+ this.v4AccessToken = data.accessToken;
7539
7565
  this.v4AccessTokenExpiresAt = addMinutes(now, 5).getTime();
7540
7566
  localStorage.setItem(
7541
7567
  this.storageKey,
7542
7568
  JSON.stringify({ version, session: data.session })
7543
7569
  );
7544
- return;
7570
+ return true;
7545
7571
  }
7546
7572
  localStorage.setItem(
7547
7573
  this.storageKey,
@@ -7552,6 +7578,16 @@ var SessionManager = class {
7552
7578
  refreshTokenExpiresAt: subMinutes(addDays(now, 30), 2).getTime()
7553
7579
  })
7554
7580
  );
7581
+ return true;
7582
+ }
7583
+ malformedResponseError() {
7584
+ return {
7585
+ ok: false,
7586
+ error: {
7587
+ name: "UnknownError",
7588
+ message: "Something went wrong."
7589
+ }
7590
+ };
7555
7591
  }
7556
7592
  async create(input, version = 1) {
7557
7593
  if (this.isServer) {
@@ -7589,7 +7625,7 @@ var SessionManager = class {
7589
7625
  maxAttempt: 5
7590
7626
  })();
7591
7627
  if (!r1.ok) return r1;
7592
- this.persist(r1.data, version);
7628
+ if (!this.persist(r1.data, version)) return this.malformedResponseError();
7593
7629
  return {
7594
7630
  ok: true,
7595
7631
  data: null
@@ -7598,7 +7634,7 @@ var SessionManager = class {
7598
7634
  if (input.type === "MOBILE_NUMBER") {
7599
7635
  const res2 = await this.authService.createSession(input, version);
7600
7636
  if (res2.ok) {
7601
- this.persist(res2.data, version);
7637
+ if (!this.persist(res2.data, version)) return this.malformedResponseError();
7602
7638
  return {
7603
7639
  ok: true,
7604
7640
  data: null
@@ -7616,7 +7652,7 @@ var SessionManager = class {
7616
7652
  version
7617
7653
  );
7618
7654
  if (res2.ok) {
7619
- this.persist(res2.data, version);
7655
+ if (!this.persist(res2.data, version)) return this.malformedResponseError();
7620
7656
  return {
7621
7657
  ok: true,
7622
7658
  data: null
@@ -7628,7 +7664,7 @@ var SessionManager = class {
7628
7664
  localStorage.setItem(this.platformStorageKey, "CABINET");
7629
7665
  const res2 = await this.authService.createSession(input, version);
7630
7666
  if (res2.ok) {
7631
- this.persist(res2.data, version);
7667
+ if (!this.persist(res2.data, version)) return this.malformedResponseError();
7632
7668
  return {
7633
7669
  ok: true,
7634
7670
  data: null
@@ -7646,7 +7682,7 @@ var SessionManager = class {
7646
7682
  }
7647
7683
  };
7648
7684
  }
7649
- this.persist(res.data, version);
7685
+ if (!this.persist(res.data, version)) return this.malformedResponseError();
7650
7686
  return {
7651
7687
  ok: true,
7652
7688
  data: null
@@ -7657,8 +7693,8 @@ var SessionManager = class {
7657
7693
  if (res.ok) {
7658
7694
  if (this.isServer) {
7659
7695
  this.logger.warn("'localStorage' is not available on the server.");
7660
- } else {
7661
- this.persist(res.data, 1);
7696
+ } else if (!this.persist(res.data, 1)) {
7697
+ return this.malformedResponseError();
7662
7698
  }
7663
7699
  return { ok: true };
7664
7700
  } else {
@@ -7837,6 +7873,15 @@ var SessionManager = class {
7837
7873
  return await this.refreshV4(session);
7838
7874
  }
7839
7875
  async refreshV4(session) {
7876
+ if (this.v4RefreshPromise) return await this.v4RefreshPromise;
7877
+ this.v4RefreshPromise = this.requestV4Refresh(session);
7878
+ try {
7879
+ return await this.v4RefreshPromise;
7880
+ } finally {
7881
+ this.v4RefreshPromise = null;
7882
+ }
7883
+ }
7884
+ async requestV4Refresh(session) {
7840
7885
  this.logger.info("Refreshing session...");
7841
7886
  this.refreshing = true;
7842
7887
  const res = await this.authService.refreshSession(void 0, 4);
@@ -7871,9 +7916,18 @@ var SessionManager = class {
7871
7916
  this.logger.warn("'localStorage' is not available on the server.");
7872
7917
  return;
7873
7918
  }
7919
+ let version = 1;
7920
+ const val = localStorage.getItem(this.storageKey);
7921
+ if (val) {
7922
+ try {
7923
+ const stored = JSON.parse(val);
7924
+ version = stored.version ?? 1;
7925
+ } catch {
7926
+ }
7927
+ }
7874
7928
  const res = await this.get();
7875
7929
  if (res.data?.accessToken) {
7876
- await this.authService.destroySession(res.data.accessToken);
7930
+ await this.authService.destroySession(res.data.accessToken, version);
7877
7931
  }
7878
7932
  this.v4AccessToken = null;
7879
7933
  this.v4AccessTokenExpiresAt = null;