zek 14.2.60 → 14.2.64

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/fesm2020/zek.mjs CHANGED
@@ -2,7 +2,7 @@ import * as i0 from '@angular/core';
2
2
  import { Injectable, InjectionToken, Inject, NgModule, Directive, Input, EventEmitter, Output, ViewChild, Pipe, Component, forwardRef, ViewEncapsulation, ChangeDetectionStrategy, Optional, HostListener } from '@angular/core';
3
3
  import * as i1 from '@angular/router';
4
4
  import { NavigationStart, PRIMARY_OUTLET, RouterModule } from '@angular/router';
5
- import { BehaviorSubject, catchError, Subject, of, firstValueFrom, ReplaySubject, timer } from 'rxjs';
5
+ import { BehaviorSubject, Subject, catchError, of, firstValueFrom, ReplaySubject, timer } from 'rxjs';
6
6
  import * as i1$1 from '@ngx-translate/core';
7
7
  import { TranslateModule } from '@ngx-translate/core';
8
8
  import * as i1$2 from '@angular/common/http';
@@ -335,6 +335,12 @@ class Convert {
335
335
  }
336
336
  return false;
337
337
  }
338
+ static parseNumber(value) {
339
+ if (typeof value === 'undefined' || value == null || (typeof value === 'string' && value.length === 0))
340
+ return null;
341
+ let n = Number(value);
342
+ return !isNaN(n) ? n : null;
343
+ }
338
344
  static toNumber(value, defaultValue = 0) {
339
345
  return this.isNumber(value) ? Number(value) : defaultValue;
340
346
  }
@@ -1110,33 +1116,51 @@ class UrlHelper {
1110
1116
  class AuthService {
1111
1117
  constructor() {
1112
1118
  this._isInitialized = false;
1113
- this._subject = new BehaviorSubject(false); //todo check if need BehaviorSubject
1114
1119
  this._oldValueIsAuthenticated = false;
1115
1120
  this._user = null;
1116
1121
  }
1117
1122
  get user() {
1118
1123
  if (!this._isInitialized) {
1119
- let tmp = StorageHelper.get('login');
1120
- if (tmp) {
1121
- tmp.expired = DateHelper.parseDate(tmp.expired);
1122
- tmp.refreshTokenTime = DateHelper.parseDate(tmp.refreshTokenTime);
1124
+ let user = StorageHelper.get('login');
1125
+ if (user) {
1126
+ user.id = ObjectHelper.isDefined(user.id) ? Convert.parseNumber(user.id) : user.id;
1127
+ user.expired = ObjectHelper.isDefined(user.expired) ? DateHelper.parseDate(user.expired) : user.expired;
1128
+ user.refreshTokenTime = ObjectHelper.isDefined(user.refreshTokenTime) ? DateHelper.parseDate(user.refreshTokenTime) : user.refreshTokenTime;
1129
+ if (Array.isArray(user.roles)) {
1130
+ user.roles = user.roles.map(function (e) { return e ? e.toUpperCase() : e; });
1131
+ }
1123
1132
  }
1124
- this._user = tmp;
1133
+ this._user = user;
1125
1134
  this._isInitialized = true;
1135
+ this._starRefreshTokenTimer();
1126
1136
  }
1127
1137
  return this._user;
1128
1138
  }
1129
- // private _expired: Date | null = null;
1130
- // private _refreshTokenExpired: Date | null = null;
1139
+ _starRefreshTokenTimer() {
1140
+ this._stopRefreshTokenTimer();
1141
+ let user = this.user;
1142
+ if (user && user.refreshTokenTime) {
1143
+ const timeout = user.refreshTokenTime.getTime() - Date.now(); // - (60 * 1000);
1144
+ this._refreshTokenTimeout = setTimeout(() => {
1145
+ if (this._onRefreshTokenSubject) {
1146
+ this._onRefreshTokenSubject.next();
1147
+ }
1148
+ }, timeout);
1149
+ }
1150
+ }
1151
+ _stopRefreshTokenTimer() {
1152
+ if (typeof this._refreshTokenTimeout === 'number') {
1153
+ clearTimeout(this._refreshTokenTimeout);
1154
+ }
1155
+ }
1131
1156
  isAuthenticated() {
1132
1157
  let expired = this.getExpired() || new Date(0);
1133
1158
  const newValue = new Date() < expired;
1134
- if (this._oldValueIsAuthenticated != newValue) {
1159
+ if (this._oldValueIsAuthenticated !== newValue) {
1135
1160
  this._oldValueIsAuthenticated = newValue;
1136
- if (this.onSignedInSubject) {
1137
- this.onSignedInSubject.next(newValue);
1161
+ if (this._onSignedInSubject) {
1162
+ this._onSignedInSubject.next(newValue);
1138
1163
  }
1139
- this._subject.next(newValue);
1140
1164
  //if user is signed in and expired we need to logout (remove from localStorage)
1141
1165
  if (!newValue) {
1142
1166
  this.logout();
@@ -1145,41 +1169,42 @@ class AuthService {
1145
1169
  }
1146
1170
  return newValue;
1147
1171
  }
1148
- /**
1149
- * @deprecated The method should not be used. please use onSignedIn
1150
- */
1151
- isSignedIn() {
1152
- return this._subject.asObservable();
1153
- }
1154
1172
  get onSignedIn() {
1155
- if (!this.onSignedInSubject) {
1156
- this.onSignedInSubject = new BehaviorSubject(false);
1157
- this.onSignedInObservable = this.onSignedInSubject.asObservable();
1173
+ if (!this._onSignedInSubject) {
1174
+ this._onSignedInSubject = new BehaviorSubject(false);
1175
+ this._onSignedInObservable = this._onSignedInSubject.asObservable();
1158
1176
  }
1159
- if (!this.onSignedInObservable)
1160
- throw new Error("onExecuteObservable is undefined");
1161
- return this.onSignedInObservable;
1177
+ if (!this._onSignedInObservable)
1178
+ throw new Error("_onExecuteObservable is undefined");
1179
+ return this._onSignedInObservable;
1162
1180
  }
1163
- login(user) {
1164
- if (user) {
1165
- if (Array.isArray(user.roles)) {
1166
- user.roles = user.roles.map(function (e) { return e.toUpperCase(); });
1167
- }
1181
+ get onRefreshToken() {
1182
+ if (!this._onRefreshTokenSubject) {
1183
+ this._onRefreshTokenSubject = new Subject();
1184
+ this._onRefreshTokenObservable = this._onRefreshTokenSubject.asObservable();
1168
1185
  }
1186
+ if (!this._onRefreshTokenObservable)
1187
+ throw new Error("onRefreshTokenObservable is undefined");
1188
+ return this._onRefreshTokenObservable;
1189
+ }
1190
+ login(user) {
1191
+ // if (user) {
1192
+ // // I think we don't need this
1193
+ // user.id = ObjectHelper.isDefined(user.id) ? Convert.parseNumber(user.id) : user.id;
1194
+ // user.expired = ObjectHelper.isDefined(user.expired) ? DateHelper.parseDate(user.expired) : user.expired;
1195
+ // user.refreshTokenTime = ObjectHelper.isDefined(user.refreshTokenTime) ? DateHelper.parseDate(user.refreshTokenTime) : user.refreshTokenTime;
1196
+ // if (Array.isArray(user.roles)) {
1197
+ // user.roles = user.roles.map(function (e) { return e.toUpperCase(); });
1198
+ // }
1199
+ // }
1169
1200
  StorageHelper.set('login', user);
1170
1201
  this._user = null;
1171
- this._isInitialized = false;
1172
- this.isAuthenticated(); //this method need to execute subject.next();
1202
+ this._isInitialized = false; //user get method will init user
1203
+ this.isAuthenticated(); //this method need to execute subject.next();
1173
1204
  }
1174
1205
  logout() {
1175
1206
  this.login(null);
1176
1207
  }
1177
- // getUser(): LoginToken | null {
1178
- // if (!this.model) {
1179
- // this.model = StorageHelper.get('login');
1180
- // }
1181
- // return this.model;
1182
- // }
1183
1208
  getExpired() {
1184
1209
  let user = this.user;
1185
1210
  if (user) {