ng-ipa-library 1.1.1 → 1.2.0

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.
@@ -1297,6 +1297,21 @@ class AuthService {
1297
1297
  exp: decodedToken.exp,
1298
1298
  });
1299
1299
  }
1300
+ getTokenFromHidden() {
1301
+ return (document.querySelector('[id$=hfToken]').value ?? '');
1302
+ }
1303
+ getDecodedTokenFromHidden() {
1304
+ return this.decodeJwt(this.getTokenFromHidden());
1305
+ }
1306
+ setCurrentUserFromHidden() {
1307
+ const decodedToken = this.getDecodedTokenFromHidden();
1308
+ this.currentUser.next({
1309
+ nameid: decodedToken.nameid,
1310
+ email: decodedToken.email,
1311
+ given_name: decodedToken.given_name,
1312
+ exp: decodedToken.exp,
1313
+ });
1314
+ }
1300
1315
  setCurrentUserUrl(url) {
1301
1316
  this.currentUserUrl = url;
1302
1317
  }
@@ -1390,6 +1405,75 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.2", ngImpor
1390
1405
  type: Injectable
1391
1406
  }], ctorParameters: function () { return [{ type: AuthService }, { type: i2$4.HttpClient }, { type: LoaderService }]; } });
1392
1407
 
1408
+ class myIPATokenInterceptor {
1409
+ constructor(authService, http, loaderService) {
1410
+ this.authService = authService;
1411
+ this.http = http;
1412
+ this.loaderService = loaderService;
1413
+ this.refresh = true;
1414
+ this.loaderService.addExceptionUrl({
1415
+ path: '/api/users?refresh',
1416
+ method: 'GET',
1417
+ });
1418
+ }
1419
+ intercept(request, next) {
1420
+ if (this.isException(request)) {
1421
+ return next.handle(request);
1422
+ }
1423
+ const token = this.authService.getTokenFromHidden();
1424
+ if (token) {
1425
+ this.isTokenExpired(token);
1426
+ request = request.clone({
1427
+ setHeaders: {
1428
+ Authorization: `Bearer ${token}`,
1429
+ },
1430
+ });
1431
+ }
1432
+ return next.handle(request);
1433
+ }
1434
+ isTokenExpired(token) {
1435
+ const redirectPath = location.pathname;
1436
+ const decodedToken = this.authService.getDecodedTokenFromHidden();
1437
+ const expDate = new Date((decodedToken.exp ?? 0) * 1000);
1438
+ if (expDate < new Date()) {
1439
+ location.replace('/ar/login?redirect=' + redirectPath);
1440
+ return;
1441
+ }
1442
+ else {
1443
+ if (this.authService.currentUserUrl) {
1444
+ if (this.refresh) {
1445
+ this.http
1446
+ .get(this.authService.currentUserUrl + `/api/users?refresh`, {
1447
+ headers: { Authorization: `Bearer ${token}` },
1448
+ })
1449
+ .subscribe((user) => {
1450
+ localStorage.setItem('token', user.token);
1451
+ this.refresh = false;
1452
+ setTimeout(() => {
1453
+ this.refresh = true;
1454
+ }, 60 * 1000);
1455
+ });
1456
+ }
1457
+ }
1458
+ else {
1459
+ throw new Error("provide current base user api url using this function authService.setCurrentUserUrl('https://...')");
1460
+ }
1461
+ }
1462
+ }
1463
+ isException(request) {
1464
+ if (request.url.includes('/api/users?refresh') &&
1465
+ request.method === 'GET') {
1466
+ return true;
1467
+ }
1468
+ return false;
1469
+ }
1470
+ }
1471
+ myIPATokenInterceptor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: myIPATokenInterceptor, deps: [{ token: AuthService }, { token: i2$4.HttpClient }, { token: LoaderService }], target: i0.ɵɵFactoryTarget.Injectable });
1472
+ myIPATokenInterceptor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: myIPATokenInterceptor });
1473
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.2", ngImport: i0, type: myIPATokenInterceptor, decorators: [{
1474
+ type: Injectable
1475
+ }], ctorParameters: function () { return [{ type: AuthService }, { type: i2$4.HttpClient }, { type: LoaderService }]; } });
1476
+
1393
1477
  class BreadcrumbsService {
1394
1478
  constructor() { }
1395
1479
  setPageTitle(text) {
@@ -1608,5 +1692,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.2", ngImpor
1608
1692
  * Generated bundle index. Do not edit.
1609
1693
  */
1610
1694
 
1611
- export { AuthService, BreadcrumbsService, CommonService, DropdownInputComponent, ErrorInterceptor, ErrorService, FileUploadComponent, GenerateFormComponent, GregorianDatepickerComponent, HijriDatePipe, HijriDatepickerComponent, IPAFormService, LoaderComponent, LoaderService, LoadingInterceptor, NgIPALibraryModule, PipesModule, RecaptchaComponent, ShareButtonComponent, ShareButtonModule, TextInputComponent, TextareaInputComponent, TokenInterceptor };
1695
+ export { AuthService, BreadcrumbsService, CommonService, DropdownInputComponent, ErrorInterceptor, ErrorService, FileUploadComponent, GenerateFormComponent, GregorianDatepickerComponent, HijriDatePipe, HijriDatepickerComponent, IPAFormService, LoaderComponent, LoaderService, LoadingInterceptor, NgIPALibraryModule, PipesModule, RecaptchaComponent, ShareButtonComponent, ShareButtonModule, TextInputComponent, TextareaInputComponent, TokenInterceptor, myIPATokenInterceptor };
1612
1696
  //# sourceMappingURL=ng-ipa-library.mjs.map