@evergis/api 3.0.199 → 3.0.200

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
@@ -175,6 +175,16 @@ function _get(target, property, receiver) {
175
175
  return _get(target, property, receiver || target);
176
176
  }
177
177
 
178
+ const STORAGE_TOKEN_KEY = "evergis-jwt-token";
179
+ const STORAGE_REFRESH_TOKEN_KEY = "evergis-jwt-token";
180
+
181
+ const parseJwt = token => {
182
+ const base64Url = token.split(".")[1];
183
+ const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
184
+ const jsonPayload = decodeURIComponent(atob(base64).split("").map(c => "%" + "00".concat(c.charCodeAt(0).toString(16)).slice(-2)).join(""));
185
+ return JSON.parse(jsonPayload);
186
+ };
187
+
178
188
  let HttpClient = /*#__PURE__*/function () {
179
189
  function HttpClient(options) {
180
190
  var _options$prefixUrl;
@@ -194,7 +204,30 @@ let HttpClient = /*#__PURE__*/function () {
194
204
  value: function extend(options) {
195
205
  var _options$prefixUrl2;
196
206
 
197
- this.ky = ky.extend(options);
207
+ this.ky = ky.extend(_extends({}, options, {
208
+ hooks: {
209
+ beforeRequest: [async request => {
210
+ const token = window.localStorage.getItem(STORAGE_TOKEN_KEY);
211
+ const refreshToken = window.localStorage.getItem(STORAGE_REFRESH_TOKEN_KEY);
212
+
213
+ if (token) {
214
+ var _request$headers;
215
+
216
+ const {
217
+ exp
218
+ } = parseJwt(token) || {};
219
+
220
+ if (typeof exp === 'number' && exp - new Date().getTime() < 60 * 1000) {
221
+ await this.post("/account/refresh-token", {
222
+ refreshToken
223
+ });
224
+ }
225
+
226
+ (_request$headers = request.headers) == null ? void 0 : _request$headers.set('Authorization', "Bearer " + (token || ''));
227
+ }
228
+ }]
229
+ }
230
+ }));
198
231
  this.prefixUrl = ((_options$prefixUrl2 = options.prefixUrl) == null ? void 0 : _options$prefixUrl2.toString()) || '';
199
232
  }
200
233
  }, {
@@ -1431,13 +1464,18 @@ let Account = /*#__PURE__*/function (_AccountService) {
1431
1464
 
1432
1465
  _createClass(Account, [{
1433
1466
  key: "login",
1434
- value: async function login(params, authParams) {
1467
+ value: function login(params, authParams) {
1435
1468
  if (authParams === void 0) {
1436
1469
  authParams = {};
1437
1470
  }
1438
1471
 
1439
1472
  if (params) {
1440
- await _get(_getPrototypeOf(Account.prototype), "authenticate", this).call(this, authParams, params);
1473
+ _get(_getPrototypeOf(Account.prototype), "authenticate", this).call(this, authParams, params).then(resonse => {
1474
+ if (resonse) {
1475
+ window.localStorage.setItem(STORAGE_TOKEN_KEY, resonse.token);
1476
+ window.localStorage.setItem(STORAGE_REFRESH_TOKEN_KEY, resonse.refreshToken);
1477
+ }
1478
+ });
1441
1479
  }
1442
1480
  }
1443
1481
  }, {
@@ -1456,8 +1494,10 @@ let Account = /*#__PURE__*/function (_AccountService) {
1456
1494
  }, {
1457
1495
  key: "logout",
1458
1496
  value: async function logout() {
1497
+ window.localStorage.removeItem(STORAGE_TOKEN_KEY);
1498
+ window.localStorage.removeItem(STORAGE_REFRESH_TOKEN_KEY);
1459
1499
  await this.revokeToken();
1460
- updateUserInfo(void 0);
1500
+ updateUserInfo(undefined);
1461
1501
  this.userInfo = {};
1462
1502
  }
1463
1503
  }, {
@@ -7593,7 +7633,7 @@ let Api = /*#__PURE__*/function (_EventEmitter) {
7593
7633
  } = _ref2;
7594
7634
 
7595
7635
  try {
7596
- await this.account.login(authParams, authQueryParams);
7636
+ this.account.login(authParams, authQueryParams);
7597
7637
 
7598
7638
  if (fetchUser) {
7599
7639
  await this.account.fetchCurrentUser();