@evergis/api 4.1.51 → 4.1.53

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/api.esm.js CHANGED
@@ -746,7 +746,7 @@ class Account extends AccountService {
746
746
  }
747
747
  return userInfo;
748
748
  }
749
- async login(params, authParams = {}, useJwt = false) {
749
+ async login(params, authParams = {}, useJwt = true) {
750
750
  if (params) {
751
751
  const response = await super.authenticate(authParams, params);
752
752
  if (response && useJwt) {
@@ -2783,11 +2783,10 @@ class Api extends EventEmitter {
2783
2783
  http;
2784
2784
  wsUrl;
2785
2785
  urlPath;
2786
- refreshingToken;
2786
+ refreshingToken = false;
2787
2787
  constructor({ url, wsUrl, wsKeepAlive, snappingHubUrl, http, urlPath, httpOptions, }) {
2788
2788
  super();
2789
2789
  const { hooks } = httpOptions || {};
2790
- this.refreshingToken = false;
2791
2790
  this.http =
2792
2791
  http ||
2793
2792
  new HttpClient({
@@ -2816,10 +2815,15 @@ class Api extends EventEmitter {
2816
2815
  retryCount === 1) {
2817
2816
  switch (error.response.status) {
2818
2817
  case 401:
2818
+ if (this.refreshingToken) {
2819
+ const currentToken = window.localStorage.getItem(STORAGE_TOKEN_KEY);
2820
+ request.headers?.set("Authorization", `Bearer ${currentToken || ""}`);
2821
+ return;
2822
+ }
2823
+ this.refreshingToken = true;
2819
2824
  try {
2820
2825
  const refreshToken = window.localStorage.getItem(STORAGE_REFRESH_TOKEN_KEY);
2821
- if (!this.refreshingToken && refreshToken) {
2822
- this.refreshingToken = true;
2826
+ if (refreshToken) {
2823
2827
  const refreshTokenResponse = await this.account.refreshToken({
2824
2828
  refreshToken,
2825
2829
  });
@@ -2828,12 +2832,17 @@ class Api extends EventEmitter {
2828
2832
  window.localStorage.setItem(STORAGE_REFRESH_TOKEN_KEY, refreshTokenResponse.refreshToken);
2829
2833
  }
2830
2834
  request.headers?.set("Authorization", `Bearer ${refreshTokenResponse.token || ""}`);
2831
- this.refreshingToken = false;
2835
+ }
2836
+ else {
2837
+ await this.logout();
2832
2838
  }
2833
2839
  }
2834
2840
  catch (error) {
2835
2841
  await this.logout();
2836
2842
  }
2843
+ finally {
2844
+ this.refreshingToken = false;
2845
+ }
2837
2846
  break;
2838
2847
  }
2839
2848
  }