@evergis/api 3.0.199 → 3.0.201

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,9 @@ 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-refresh-token";
180
+
178
181
  let HttpClient = /*#__PURE__*/function () {
179
182
  function HttpClient(options) {
180
183
  var _options$prefixUrl;
@@ -1431,13 +1434,18 @@ let Account = /*#__PURE__*/function (_AccountService) {
1431
1434
 
1432
1435
  _createClass(Account, [{
1433
1436
  key: "login",
1434
- value: async function login(params, authParams) {
1437
+ value: function login(params, authParams) {
1435
1438
  if (authParams === void 0) {
1436
1439
  authParams = {};
1437
1440
  }
1438
1441
 
1439
1442
  if (params) {
1440
- await _get(_getPrototypeOf(Account.prototype), "authenticate", this).call(this, authParams, params);
1443
+ _get(_getPrototypeOf(Account.prototype), "authenticate", this).call(this, authParams, params).then(resonse => {
1444
+ if (resonse) {
1445
+ window.localStorage.setItem(STORAGE_TOKEN_KEY, resonse.token);
1446
+ window.localStorage.setItem(STORAGE_REFRESH_TOKEN_KEY, resonse.refreshToken);
1447
+ }
1448
+ });
1441
1449
  }
1442
1450
  }
1443
1451
  }, {
@@ -1456,8 +1464,10 @@ let Account = /*#__PURE__*/function (_AccountService) {
1456
1464
  }, {
1457
1465
  key: "logout",
1458
1466
  value: async function logout() {
1467
+ window.localStorage.removeItem(STORAGE_TOKEN_KEY);
1468
+ window.localStorage.removeItem(STORAGE_REFRESH_TOKEN_KEY);
1459
1469
  await this.revokeToken();
1460
- updateUserInfo(void 0);
1470
+ updateUserInfo(undefined);
1461
1471
  this.userInfo = {};
1462
1472
  }
1463
1473
  }, {
@@ -7479,6 +7489,13 @@ let VectorTiles = /*#__PURE__*/function (_VectorTileService) {
7479
7489
  return VectorTiles;
7480
7490
  }(VectorTileService);
7481
7491
 
7492
+ const parseJwt = token => {
7493
+ const base64Url = token.split(".")[1];
7494
+ const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
7495
+ const jsonPayload = decodeURIComponent(atob(base64).split("").map(c => "%" + "00".concat(c.charCodeAt(0).toString(16)).slice(-2)).join(""));
7496
+ return JSON.parse(jsonPayload);
7497
+ };
7498
+
7482
7499
  var UrlPath;
7483
7500
 
7484
7501
  (function (UrlPath) {
@@ -7498,6 +7515,8 @@ let Api = /*#__PURE__*/function (_EventEmitter) {
7498
7515
  var _super = /*#__PURE__*/_createSuper(Api);
7499
7516
 
7500
7517
  function Api(_ref) {
7518
+ var _hooks$beforeRequest;
7519
+
7501
7520
  var _this;
7502
7521
 
7503
7522
  let {
@@ -7521,7 +7540,26 @@ let Api = /*#__PURE__*/function (_EventEmitter) {
7521
7540
  timeout: false
7522
7541
  }, httpOptions || {}, {
7523
7542
  hooks: _extends({}, hooks || {}, {
7524
- beforeRequest: [...(hooks && hooks.beforeRequest || [])],
7543
+ beforeRequest: [async request => {
7544
+ const token = window.localStorage.getItem(STORAGE_TOKEN_KEY);
7545
+ const refreshToken = window.localStorage.getItem(STORAGE_REFRESH_TOKEN_KEY);
7546
+
7547
+ if (token && refreshToken) {
7548
+ var _request$headers;
7549
+
7550
+ const {
7551
+ exp
7552
+ } = parseJwt(token) || {};
7553
+
7554
+ if (typeof exp === 'number' && exp - new Date().getTime() < 60 * 1000) {
7555
+ await _this.account.refreshToken({
7556
+ refreshToken
7557
+ });
7558
+ }
7559
+
7560
+ (_request$headers = request.headers) == null ? void 0 : _request$headers.set('Authorization', "Bearer " + (token || ''));
7561
+ }
7562
+ }, ...((_hooks$beforeRequest = hooks == null ? void 0 : hooks.beforeRequest) != null ? _hooks$beforeRequest : [])],
7525
7563
  afterResponse: [response => {
7526
7564
  const apiEvent = apiEventsByResponseStatus[response.status];
7527
7565
 
@@ -7530,7 +7568,7 @@ let Api = /*#__PURE__*/function (_EventEmitter) {
7530
7568
  }
7531
7569
 
7532
7570
  return response;
7533
- }, ...(hooks && hooks.afterResponse || [])]
7571
+ }, ...((hooks == null ? void 0 : hooks.afterResponse) || [])]
7534
7572
  })
7535
7573
  }));
7536
7574
  _this.url = url;
@@ -7593,7 +7631,7 @@ let Api = /*#__PURE__*/function (_EventEmitter) {
7593
7631
  } = _ref2;
7594
7632
 
7595
7633
  try {
7596
- await this.account.login(authParams, authQueryParams);
7634
+ this.account.login(authParams, authQueryParams);
7597
7635
 
7598
7636
  if (fetchUser) {
7599
7637
  await this.account.fetchCurrentUser();