zek 17.3.88 → 17.3.97

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/fesm2022/zek.mjs CHANGED
@@ -2,7 +2,7 @@ import * as i0 from '@angular/core';
2
2
  import { Injectable, inject, InjectionToken, Inject, Directive, EventEmitter, Input, Output, ViewChild, Pipe, Component, forwardRef, NgModule, HostListener, ViewEncapsulation, ChangeDetectionStrategy, Optional } from '@angular/core';
3
3
  import * as i1 from '@angular/router';
4
4
  import { Router, NavigationStart, ActivatedRoute, RouterModule } from '@angular/router';
5
- import { BehaviorSubject, Subject, of, tap, catchError, firstValueFrom, interval, timer } from 'rxjs';
5
+ import { Subject, of, tap, catchError, firstValueFrom, interval, BehaviorSubject, timer } from 'rxjs';
6
6
  import * as i2 from '@ngx-translate/core';
7
7
  import { TranslateService, TranslateModule } from '@ngx-translate/core';
8
8
  import * as i1$1 from '@angular/common/http';
@@ -1434,50 +1434,68 @@ class ValidationHelper {
1434
1434
  }
1435
1435
 
1436
1436
  class AuthService {
1437
+ static { this.USER_KEY = 'user'; }
1438
+ static { this.REFRESH_TOKEN_INTERVAL = 15 * 60 * 1000; }
1437
1439
  constructor() {
1438
- this._isInitialized = false;
1439
- this._oldValue = false;
1440
+ this._auth = false;
1440
1441
  this._user = null;
1442
+ const user = StorageHelper.get(AuthService.USER_KEY);
1443
+ if (user) {
1444
+ this._init(user);
1445
+ }
1446
+ }
1447
+ get _isAuthenticated() {
1448
+ const expired = this.getExpired();
1449
+ return new Date() < expired;
1441
1450
  }
1442
- static { this.USER_KEY = 'user'; }
1443
- // access_token
1444
1451
  get user() {
1445
- if (!this._isInitialized) {
1446
- const user = StorageHelper.get(AuthService.USER_KEY);
1447
- if (user) {
1448
- user.id = ObjectHelper.isDefined(user.id) ? Convert.parseNumber(user.id) : user.id;
1449
- user.expired = ObjectHelper.isDefined(user.expired) ? DateHelper.parseDate(user.expired) : user.expired;
1450
- user.refreshTokenTime = ObjectHelper.isDefined(user.refreshTokenTime) ? DateHelper.parseDate(user.refreshTokenTime) : user.refreshTokenTime;
1451
- if (Array.isArray(user.roles)) {
1452
- user.roles = user.roles.map(function (e) { return e ? e.toUpperCase() : e; });
1453
- }
1454
- }
1455
- this._user = user;
1456
- this._isInitialized = true;
1457
- this._starRefreshTokenTimer();
1458
- }
1459
1452
  return this._user;
1460
1453
  }
1461
- _starRefreshTokenTimer() {
1462
- this._stopRefreshTokenTimer();
1463
- const user = this._user;
1464
- if (user && user.refreshTokenTime) {
1465
- let timeout = user.refreshTokenTime.getTime() - Date.now(); // - (60 * 1000);
1466
- if (timeout < 0) {
1454
+ get isAuthenticated() {
1455
+ return this._auth;
1456
+ }
1457
+ _starTimer() {
1458
+ this._stopTimer();
1459
+ let interval = this.getExpired().getTime() - new Date().getTime();
1460
+ if (interval < 1000)
1461
+ interval = 1000;
1462
+ if (interval > 0 && this._user) {
1463
+ this._refreshTimerId = setInterval(() => {
1467
1464
  this.emitOnRefreshToken();
1465
+ }, interval);
1466
+ if (this._user) {
1467
+ this._timerId = setInterval(() => {
1468
+ this._onTick();
1469
+ }, 1000);
1468
1470
  }
1469
- const minRefreshTime = 60000; //1 min;
1470
- if (timeout < minRefreshTime) {
1471
- timeout = minRefreshTime;
1471
+ }
1472
+ }
1473
+ _stopTimer() {
1474
+ if (typeof this._timerId === 'number') {
1475
+ clearInterval(this._timerId);
1476
+ }
1477
+ }
1478
+ _onTick() {
1479
+ const newValue = this._isAuthenticated;
1480
+ if (this._auth !== newValue) {
1481
+ this._auth = newValue;
1482
+ if (!newValue) {
1483
+ this.logout();
1472
1484
  }
1473
- this._timerId = setInterval(() => {
1485
+ }
1486
+ }
1487
+ _starRefreshTokenTimer() {
1488
+ this._stopRefreshTokenTimer();
1489
+ const interval = AuthService.REFRESH_TOKEN_INTERVAL;
1490
+ if (interval > 0 && this._user) {
1491
+ this._refreshTimerId = setInterval(() => {
1474
1492
  this.emitOnRefreshToken();
1475
- }, timeout);
1493
+ }, interval);
1476
1494
  }
1477
1495
  }
1478
1496
  _stopRefreshTokenTimer() {
1479
- if (typeof this._timerId === 'number') {
1480
- clearInterval(this._timerId);
1497
+ if (typeof this._refreshTimerId === 'number') {
1498
+ clearInterval(this._refreshTimerId);
1481
1499
  }
1482
1500
  }
1483
1501
  emitOnRefreshToken() {
@@ -1485,28 +1503,14 @@ class AuthService {
1485
1503
  this._onRefreshTokenSubject.next();
1486
1504
  }
1487
1505
  }
1488
- isAuthenticated() {
1489
- const expired = this.getExpired() || new Date(0); // if getExpired is null return min JS date
1490
- const newValue = new Date() < expired;
1491
- if (this._oldValue !== newValue) {
1492
- this._oldValue = newValue;
1493
- this.emitOnSignedIn();
1494
- //if user is signed in and expired we need to logout (remove from localStorage)
1495
- if (!newValue) {
1496
- this.logout();
1497
- }
1498
- return newValue;
1499
- }
1500
- return newValue;
1501
- }
1502
1506
  emitOnSignedIn() {
1503
1507
  if (this._onSignedInSubject) {
1504
- this._onSignedInSubject.next(this._oldValue);
1508
+ this._onSignedInSubject.next(this._auth);
1505
1509
  }
1506
1510
  }
1507
1511
  get onSignedIn() {
1508
1512
  if (!this._onSignedInSubject) {
1509
- this._onSignedInSubject = new BehaviorSubject(false);
1513
+ this._onSignedInSubject = new Subject(); //(this.isAuthenticated);
1510
1514
  this._onSignedInObservable = this._onSignedInSubject.asObservable();
1511
1515
  }
1512
1516
  if (!this._onSignedInObservable)
@@ -1524,39 +1528,42 @@ class AuthService {
1524
1528
  }
1525
1529
  login(user) {
1526
1530
  StorageHelper.set(AuthService.USER_KEY, user);
1527
- this._user = null;
1528
- this._isInitialized = false; //user get method will init user
1529
- this.isAuthenticated(); //this method need to execute subject.next();
1531
+ this._init(user);
1530
1532
  }
1531
- logout() {
1532
- this.login(null);
1533
- }
1534
- getExpired() {
1535
- const user = this.user;
1533
+ _init(user) {
1536
1534
  if (user) {
1537
- return user.expired;
1535
+ //convert string to local date/time
1536
+ user.expired = ObjectHelper.isDefined(user.expired) ? DateHelper.parseDate(user.expired) : user.expired;
1537
+ if (Array.isArray(user.roles)) {
1538
+ user.roles = user.roles.map(function (e) { return e ? e.toUpperCase() : e; });
1539
+ }
1538
1540
  }
1539
- return null;
1541
+ this._user = user;
1542
+ //if old value was false and we set true then we need to emit
1543
+ if (!this._auth) {
1544
+ this._auth = true;
1545
+ this.emitOnSignedIn();
1546
+ }
1547
+ this._starTimer();
1548
+ this._starRefreshTokenTimer();
1540
1549
  }
1541
- getRefreshTokenExpired() {
1542
- const user = this.user;
1543
- if (user) {
1544
- return user.refreshTokenTime;
1550
+ logout() {
1551
+ StorageHelper.set(AuthService.USER_KEY, null);
1552
+ this._user = null;
1553
+ if (this._auth) {
1554
+ this._auth = false;
1555
+ this.emitOnSignedIn();
1545
1556
  }
1546
- return null;
1557
+ this._stopTimer();
1558
+ this._stopRefreshTokenTimer();
1559
+ }
1560
+ /**
1561
+ *
1562
+ * @returns user expiry date. if user is null returns min JS date
1563
+ */
1564
+ getExpired() {
1565
+ return this.user?.expired ?? new Date(0);
1547
1566
  }
1548
- // isInRole(allowedRoles: string[]): boolean {
1549
- // if (!allowedRoles || allowedRoles.length === 0) {
1550
- // return true;
1551
- // }
1552
- // const tmp = this.getUser();
1553
- // const roles = tmp ? tmp.roles : null;
1554
- // if (!roles || roles.length === 0) {
1555
- // return false;
1556
- // }
1557
- // let isInRole = allowedRoles.some(role => roles.indexOf(role) !== -1);
1558
- // return isInRole;
1559
- // }
1560
1567
  hasPermission(permission) {
1561
1568
  if (!permission) {
1562
1569
  return true;
@@ -1619,12 +1626,12 @@ class AuthService {
1619
1626
  }
1620
1627
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.4", ngImport: i0, type: AuthService, decorators: [{
1621
1628
  type: Injectable
1622
- }] });
1629
+ }], ctorParameters: () => [] });
1623
1630
 
1624
1631
  const zekAuthGuard = (route, state) => {
1625
1632
  const auth = inject(AuthService);
1626
1633
  const router = inject(Router);
1627
- if (!auth.isAuthenticated()) {
1634
+ if (!auth.isAuthenticated) {
1628
1635
  router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
1629
1636
  return false;
1630
1637
  }
@@ -1934,7 +1941,8 @@ class WebApiClient {
1934
1941
  this.baseUrl = baseUrl;
1935
1942
  }
1936
1943
  get(url, params) {
1937
- return this.http.get(this.baseUrl + url, { headers: this.getHeaders(), params: this.toHttpParams(params) });
1944
+ const combinedUrl = UrlHelper.combine(this.baseUrl, url);
1945
+ return this.http.get(combinedUrl, { headers: this.getHeaders(), params: this.toHttpParams(params) });
1938
1946
  /*.catch(catchError(this.handleError(url)))
1939
1947
  .do((res: Response) => {
1940
1948
  // Handle success, maybe display notification
@@ -1947,16 +1955,20 @@ class WebApiClient {
1947
1955
  */
1948
1956
  }
1949
1957
  getString(url, params) {
1950
- return this.http.get(this.baseUrl + url, { headers: this.getHeaders(), responseType: 'text', params: this.toHttpParams(params) });
1958
+ const combinedUrl = UrlHelper.combine(this.baseUrl, url);
1959
+ return this.http.get(combinedUrl, { headers: this.getHeaders(), responseType: 'text', params: this.toHttpParams(params) });
1951
1960
  }
1952
1961
  getBytes(url, params) {
1953
- return this.http.get(this.baseUrl + url, { headers: this.getHeaders(), responseType: 'arraybuffer', params: this.toHttpParams(params) });
1962
+ const combinedUrl = UrlHelper.combine(this.baseUrl, url);
1963
+ return this.http.get(combinedUrl, { headers: this.getHeaders(), responseType: 'arraybuffer', params: this.toHttpParams(params) });
1954
1964
  }
1955
1965
  getBlob(url, params) {
1956
- return this.http.get(this.baseUrl + url, { headers: this.getHeaders(), responseType: 'blob', params: this.toHttpParams(params) });
1966
+ const combinedUrl = UrlHelper.combine(this.baseUrl, url);
1967
+ return this.http.get(combinedUrl, { headers: this.getHeaders(), responseType: 'blob', params: this.toHttpParams(params) });
1957
1968
  }
1958
1969
  postBlob(url, body) {
1959
- return this.http.post(this.baseUrl + url, body, { headers: this.getHeaders(), responseType: 'blob' });
1970
+ const combinedUrl = UrlHelper.combine(this.baseUrl, url);
1971
+ return this.http.post(combinedUrl, body, { headers: this.getHeaders(), responseType: 'blob' });
1960
1972
  }
1961
1973
  toHttpParams(obj) {
1962
1974
  if (!obj)
@@ -1999,26 +2011,26 @@ class WebApiClient {
1999
2011
  // return undefined;
2000
2012
  //}
2001
2013
  post(url, body) {
2002
- return this.http.post(this.baseUrl + url, body, { headers: this.getHeaders(body) });
2014
+ return this.http.post(UrlHelper.combine(this.baseUrl, url), body, { headers: this.getHeaders(body) });
2003
2015
  //.pipe(
2004
2016
  //catchError(this.handleError(url))
2005
2017
  //);
2006
2018
  }
2007
2019
  put(url, body) {
2008
- return this.http.put(this.baseUrl + url, body, { headers: this.getHeaders(body) });
2020
+ return this.http.put(UrlHelper.combine(this.baseUrl, url), body, { headers: this.getHeaders(body) });
2009
2021
  //.pipe(
2010
2022
  //catchError(this.handleError(url))
2011
2023
  //);
2012
2024
  }
2013
2025
  delete(url, body) {
2014
- return this.http.request('delete', this.baseUrl + url, { body, headers: this.getHeaders(body) });
2026
+ return this.http.request('delete', UrlHelper.combine(this.baseUrl, url), { body, headers: this.getHeaders(body) });
2015
2027
  //return this.http.delete(environment.url + url, { headers: this.getHeaders() });
2016
2028
  //.pipe(
2017
2029
  //catchError(this.handleError(url))
2018
2030
  //);
2019
2031
  }
2020
2032
  patch(url, body) {
2021
- return this.http.patch(this.baseUrl + url, body, { headers: this.getHeaders(body) });
2033
+ return this.http.patch(UrlHelper.combine(this.baseUrl, url), body, { headers: this.getHeaders(body) });
2022
2034
  }
2023
2035
  getHeaders(body) {
2024
2036
  let httpHeaders = new HttpHeaders();