zek 14.2.59 → 14.2.60

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
@@ -1109,22 +1109,34 @@ class UrlHelper {
1109
1109
 
1110
1110
  class AuthService {
1111
1111
  constructor() {
1112
- this.subject = new BehaviorSubject(false); //todo check if need BehaviorSubject
1113
- this.model = null;
1114
- this.expired = null;
1115
- this.oldValueIsAuthenticated = false;
1112
+ this._isInitialized = false;
1113
+ this._subject = new BehaviorSubject(false); //todo check if need BehaviorSubject
1114
+ this._oldValueIsAuthenticated = false;
1115
+ this._user = null;
1116
+ }
1117
+ get user() {
1118
+ 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);
1123
+ }
1124
+ this._user = tmp;
1125
+ this._isInitialized = true;
1126
+ }
1127
+ return this._user;
1116
1128
  }
1129
+ // private _expired: Date | null = null;
1130
+ // private _refreshTokenExpired: Date | null = null;
1117
1131
  isAuthenticated() {
1118
- let expired = this.getExpired();
1119
- if (!expired)
1120
- expired = new Date(0);
1132
+ let expired = this.getExpired() || new Date(0);
1121
1133
  const newValue = new Date() < expired;
1122
- if (this.oldValueIsAuthenticated != newValue) {
1123
- this.oldValueIsAuthenticated = newValue;
1134
+ if (this._oldValueIsAuthenticated != newValue) {
1135
+ this._oldValueIsAuthenticated = newValue;
1124
1136
  if (this.onSignedInSubject) {
1125
1137
  this.onSignedInSubject.next(newValue);
1126
1138
  }
1127
- this.subject.next(newValue);
1139
+ this._subject.next(newValue);
1128
1140
  //if user is signed in and expired we need to logout (remove from localStorage)
1129
1141
  if (!newValue) {
1130
1142
  this.logout();
@@ -1137,7 +1149,7 @@ class AuthService {
1137
1149
  * @deprecated The method should not be used. please use onSignedIn
1138
1150
  */
1139
1151
  isSignedIn() {
1140
- return this.subject.asObservable();
1152
+ return this._subject.asObservable();
1141
1153
  }
1142
1154
  get onSignedIn() {
1143
1155
  if (!this.onSignedInSubject) {
@@ -1148,39 +1160,39 @@ class AuthService {
1148
1160
  throw new Error("onExecuteObservable is undefined");
1149
1161
  return this.onSignedInObservable;
1150
1162
  }
1151
- login(tmp) {
1152
- this.model = undefined;
1153
- if (tmp) {
1154
- //Globals.setServerDate(tmpModel.currentDateTime);
1155
- //this.permissionsService.flushPermissions();
1156
- if (tmp.roles) {
1157
- tmp.roles = tmp.roles.map(function (e) { return e.toUpperCase(); });
1158
- //this.permissionsService.loadPermissions(model.roles);
1163
+ login(user) {
1164
+ if (user) {
1165
+ if (Array.isArray(user.roles)) {
1166
+ user.roles = user.roles.map(function (e) { return e.toUpperCase(); });
1159
1167
  }
1160
1168
  }
1161
- //delete tmpModel.currentDateTime;
1162
- this.model = tmp;
1163
- this.expired = null;
1164
- StorageHelper.set('login', this.model);
1169
+ StorageHelper.set('login', user);
1170
+ this._user = null;
1171
+ this._isInitialized = false;
1165
1172
  this.isAuthenticated(); //this method need to execute subject.next();
1166
1173
  }
1167
1174
  logout() {
1168
1175
  this.login(null);
1169
1176
  }
1170
- getUser() {
1171
- if (!this.model) {
1172
- this.model = StorageHelper.get('login');
1177
+ // getUser(): LoginToken | null {
1178
+ // if (!this.model) {
1179
+ // this.model = StorageHelper.get('login');
1180
+ // }
1181
+ // return this.model;
1182
+ // }
1183
+ getExpired() {
1184
+ let user = this.user;
1185
+ if (user) {
1186
+ return user.expired;
1173
1187
  }
1174
- return this.model;
1188
+ return null;
1175
1189
  }
1176
- getExpired() {
1177
- if (!this.expired) {
1178
- const tmp = this.getUser();
1179
- if (tmp && tmp.expired) {
1180
- this.expired = new Date(tmp.expired);
1181
- }
1190
+ getRefreshTokenExpired() {
1191
+ let user = this.user;
1192
+ if (user) {
1193
+ return user.refreshTokenTime;
1182
1194
  }
1183
- return this.expired;
1195
+ return null;
1184
1196
  }
1185
1197
  // isInRole(allowedRoles: string[]): boolean {
1186
1198
  // if (!allowedRoles || allowedRoles.length === 0) {
@@ -1216,8 +1228,8 @@ class AuthService {
1216
1228
  if (!permissions || permissions.length === 0) {
1217
1229
  return true;
1218
1230
  }
1219
- const tmp = this.getUser();
1220
- const userPermissions = tmp ? tmp.permissions : null;
1231
+ const user = this.user;
1232
+ const userPermissions = user ? user.permissions : null;
1221
1233
  if (!userPermissions) {
1222
1234
  return false;
1223
1235
  }
@@ -1728,7 +1740,7 @@ class WebApiClient {
1728
1740
  getHeaders() {
1729
1741
  let httpHeaders = new HttpHeaders();
1730
1742
  httpHeaders = httpHeaders.set('Content-Type', 'application/json');
1731
- const tmp = this.authService.getUser();
1743
+ const tmp = this.authService.user;
1732
1744
  const token = tmp ? tmp.token : undefined;
1733
1745
  if (token)
1734
1746
  httpHeaders = httpHeaders.set('Authorization', token);