zek 14.2.59 → 14.2.62
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/esm2020/lib/models/login.model.mjs +1 -1
- package/esm2020/lib/services/auth.service.mjs +84 -53
- package/esm2020/lib/services/web.api.mjs +2 -2
- package/esm2020/lib/utils/convert.mjs +7 -1
- package/fesm2015/zek.mjs +91 -54
- package/fesm2015/zek.mjs.map +1 -1
- package/fesm2020/zek.mjs +91 -54
- package/fesm2020/zek.mjs.map +1 -1
- package/lib/models/login.model.d.ts +9 -2
- package/lib/services/auth.service.d.ts +16 -14
- package/lib/utils/convert.d.ts +1 -0
- package/package.json +1 -1
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,
|
|
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
|
}
|
|
@@ -1109,22 +1115,53 @@ class UrlHelper {
|
|
|
1109
1115
|
|
|
1110
1116
|
class AuthService {
|
|
1111
1117
|
constructor() {
|
|
1112
|
-
this.
|
|
1113
|
-
this.
|
|
1114
|
-
this.
|
|
1115
|
-
|
|
1118
|
+
this._isInitialized = false;
|
|
1119
|
+
this._oldValueIsAuthenticated = false;
|
|
1120
|
+
this._user = null;
|
|
1121
|
+
}
|
|
1122
|
+
get user() {
|
|
1123
|
+
if (!this._isInitialized) {
|
|
1124
|
+
let tmp = StorageHelper.get('login');
|
|
1125
|
+
if (tmp) {
|
|
1126
|
+
// let user:LoginUser = {
|
|
1127
|
+
// id: +tmp.id,
|
|
1128
|
+
// expired: DateHelper.parseDate(tmp.expired)
|
|
1129
|
+
// }
|
|
1130
|
+
tmp.id = Convert.parseNumber(tmp.id);
|
|
1131
|
+
tmp.expired = DateHelper.parseDate(tmp.expired);
|
|
1132
|
+
tmp.refreshTokenTime = DateHelper.parseDate(tmp.refreshTokenTime);
|
|
1133
|
+
}
|
|
1134
|
+
this._user = tmp;
|
|
1135
|
+
this._isInitialized = true;
|
|
1136
|
+
this._starRefreshTokenTimer();
|
|
1137
|
+
}
|
|
1138
|
+
return this._user;
|
|
1139
|
+
}
|
|
1140
|
+
_starRefreshTokenTimer() {
|
|
1141
|
+
this._stopRefreshTokenTimer();
|
|
1142
|
+
let user = this.user;
|
|
1143
|
+
if (user && user.refreshTokenTime) {
|
|
1144
|
+
const timeout = user.refreshTokenTime.getTime() - Date.now(); // - (60 * 1000);
|
|
1145
|
+
this._refreshTokenTimeout = setTimeout(() => {
|
|
1146
|
+
if (this._onRefreshTokenSubject) {
|
|
1147
|
+
this._onRefreshTokenSubject.next();
|
|
1148
|
+
}
|
|
1149
|
+
}, timeout);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
_stopRefreshTokenTimer() {
|
|
1153
|
+
if (typeof this._refreshTokenTimeout === 'number') {
|
|
1154
|
+
clearTimeout(this._refreshTokenTimeout);
|
|
1155
|
+
}
|
|
1116
1156
|
}
|
|
1117
1157
|
isAuthenticated() {
|
|
1118
|
-
let expired = this.getExpired();
|
|
1119
|
-
if (!expired)
|
|
1120
|
-
expired = new Date(0);
|
|
1158
|
+
let expired = this.getExpired() || new Date(0);
|
|
1121
1159
|
const newValue = new Date() < expired;
|
|
1122
|
-
if (this.
|
|
1123
|
-
this.
|
|
1124
|
-
if (this.
|
|
1125
|
-
this.
|
|
1160
|
+
if (this._oldValueIsAuthenticated !== newValue) {
|
|
1161
|
+
this._oldValueIsAuthenticated = newValue;
|
|
1162
|
+
if (this._onSignedInSubject) {
|
|
1163
|
+
this._onSignedInSubject.next(newValue);
|
|
1126
1164
|
}
|
|
1127
|
-
this.subject.next(newValue);
|
|
1128
1165
|
//if user is signed in and expired we need to logout (remove from localStorage)
|
|
1129
1166
|
if (!newValue) {
|
|
1130
1167
|
this.logout();
|
|
@@ -1133,54 +1170,54 @@ class AuthService {
|
|
|
1133
1170
|
}
|
|
1134
1171
|
return newValue;
|
|
1135
1172
|
}
|
|
1136
|
-
/**
|
|
1137
|
-
* @deprecated The method should not be used. please use onSignedIn
|
|
1138
|
-
*/
|
|
1139
|
-
isSignedIn() {
|
|
1140
|
-
return this.subject.asObservable();
|
|
1141
|
-
}
|
|
1142
1173
|
get onSignedIn() {
|
|
1143
|
-
if (!this.
|
|
1144
|
-
this.
|
|
1145
|
-
this.
|
|
1146
|
-
}
|
|
1147
|
-
if (!this.
|
|
1148
|
-
throw new Error("
|
|
1149
|
-
return this.
|
|
1150
|
-
}
|
|
1151
|
-
|
|
1152
|
-
this.
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1174
|
+
if (!this._onSignedInSubject) {
|
|
1175
|
+
this._onSignedInSubject = new BehaviorSubject(false);
|
|
1176
|
+
this._onSignedInObservable = this._onSignedInSubject.asObservable();
|
|
1177
|
+
}
|
|
1178
|
+
if (!this._onSignedInObservable)
|
|
1179
|
+
throw new Error("_onExecuteObservable is undefined");
|
|
1180
|
+
return this._onSignedInObservable;
|
|
1181
|
+
}
|
|
1182
|
+
get onRefreshToken() {
|
|
1183
|
+
if (!this._onRefreshTokenSubject) {
|
|
1184
|
+
this._onRefreshTokenSubject = new Subject();
|
|
1185
|
+
this._onRefreshTokenObservable = this._onRefreshTokenSubject.asObservable();
|
|
1186
|
+
}
|
|
1187
|
+
if (!this._onRefreshTokenObservable)
|
|
1188
|
+
throw new Error("onRefreshTokenObservable is undefined");
|
|
1189
|
+
return this._onRefreshTokenObservable;
|
|
1190
|
+
}
|
|
1191
|
+
login(user) {
|
|
1192
|
+
if (user) {
|
|
1193
|
+
user.id = Convert.parseNumber(user.id);
|
|
1194
|
+
user.expired = DateHelper.parseDate(user.expired);
|
|
1195
|
+
user.refreshTokenTime = DateHelper.parseDate(user.refreshTokenTime);
|
|
1196
|
+
if (Array.isArray(user.roles)) {
|
|
1197
|
+
user.roles = user.roles.map(function (e) { return e.toUpperCase(); });
|
|
1159
1198
|
}
|
|
1160
1199
|
}
|
|
1161
|
-
|
|
1162
|
-
this.
|
|
1163
|
-
this.
|
|
1164
|
-
|
|
1165
|
-
this.isAuthenticated(); //this method need to execute subject.next();
|
|
1200
|
+
StorageHelper.set('login', user);
|
|
1201
|
+
this._user = null;
|
|
1202
|
+
this._isInitialized = false; //user get method will init user
|
|
1203
|
+
this.isAuthenticated(); //this method need to execute subject.next();
|
|
1166
1204
|
}
|
|
1167
1205
|
logout() {
|
|
1168
1206
|
this.login(null);
|
|
1169
1207
|
}
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1208
|
+
getExpired() {
|
|
1209
|
+
let user = this.user;
|
|
1210
|
+
if (user) {
|
|
1211
|
+
return user.expired;
|
|
1173
1212
|
}
|
|
1174
|
-
return
|
|
1213
|
+
return null;
|
|
1175
1214
|
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
this.expired = new Date(tmp.expired);
|
|
1181
|
-
}
|
|
1215
|
+
getRefreshTokenExpired() {
|
|
1216
|
+
let user = this.user;
|
|
1217
|
+
if (user) {
|
|
1218
|
+
return user.refreshTokenTime;
|
|
1182
1219
|
}
|
|
1183
|
-
return
|
|
1220
|
+
return null;
|
|
1184
1221
|
}
|
|
1185
1222
|
// isInRole(allowedRoles: string[]): boolean {
|
|
1186
1223
|
// if (!allowedRoles || allowedRoles.length === 0) {
|
|
@@ -1216,8 +1253,8 @@ class AuthService {
|
|
|
1216
1253
|
if (!permissions || permissions.length === 0) {
|
|
1217
1254
|
return true;
|
|
1218
1255
|
}
|
|
1219
|
-
const
|
|
1220
|
-
const userPermissions =
|
|
1256
|
+
const user = this.user;
|
|
1257
|
+
const userPermissions = user ? user.permissions : null;
|
|
1221
1258
|
if (!userPermissions) {
|
|
1222
1259
|
return false;
|
|
1223
1260
|
}
|
|
@@ -1728,7 +1765,7 @@ class WebApiClient {
|
|
|
1728
1765
|
getHeaders() {
|
|
1729
1766
|
let httpHeaders = new HttpHeaders();
|
|
1730
1767
|
httpHeaders = httpHeaders.set('Content-Type', 'application/json');
|
|
1731
|
-
const tmp = this.authService.
|
|
1768
|
+
const tmp = this.authService.user;
|
|
1732
1769
|
const token = tmp ? tmp.token : undefined;
|
|
1733
1770
|
if (token)
|
|
1734
1771
|
httpHeaders = httpHeaders.set('Authorization', token);
|