@smartbit4all/ng-client 2.0.10 → 2.0.11

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.
@@ -1,9 +1,9 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { InjectionToken, Injectable, Optional, Inject, PLATFORM_ID, NgModule, SkipSelf, Component, Pipe, CUSTOM_ELEMENTS_SCHEMA, ViewChild, Input, ViewChildren } from '@angular/core';
3
- import { Subject, takeUntil, map, catchError, throwError, from } from 'rxjs';
4
- import { SmartSubject } from '@smartbit4all/subject';
5
3
  import * as i1 from '@angular/common/http';
6
- import { HttpHeaders, HttpContext, HttpParams, HttpEventType, HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
4
+ import { HttpHeaders, HttpContext, HttpErrorResponse, HttpParams, HttpEventType, HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
5
+ import { Subject, lastValueFrom, takeUntil, map, catchError, throwError, from } from 'rxjs';
6
+ import { SmartSubject } from '@smartbit4all/subject';
7
7
  import * as i4 from '@angular/common';
8
8
  import { isPlatformBrowser, DOCUMENT, DatePipe, CommonModule } from '@angular/common';
9
9
  import * as i2 from '@angular/material/button';
@@ -765,7 +765,7 @@ class SmartSessionService {
765
765
  }
766
766
  async handleSessionError(sessionError) {
767
767
  if (this.sessionHandlerService) {
768
- await this.sessionHandlerService.handleSessionError(sessionError);
768
+ return await this.sessionHandlerService.handleSessionError(sessionError);
769
769
  }
770
770
  throw new Error(`sessionHandlerService was not initialized. Error: ${sessionError.code}`);
771
771
  }
@@ -860,11 +860,7 @@ class SmartSessionService {
860
860
  async startSession() {
861
861
  let sessionInfo = await this.apiService.startSession().toPromise();
862
862
  if (sessionInfo) {
863
- this.sessionInfoData = sessionInfo;
864
- this.token = sessionInfo.sid;
865
- this.saveTokenToLocalStorage();
866
- this.setAuthenticationStatus();
867
- this.setUpTokenForSessionService();
863
+ this.updateSessionInfo(sessionInfo);
868
864
  }
869
865
  return this.sessionInfoData;
870
866
  }
@@ -883,26 +879,34 @@ class SmartSessionService {
883
879
  if (!refreshToken) {
884
880
  return await this.startSession();
885
881
  }
886
- let sessionInfo = await this.apiService
887
- .refreshSession({
888
- refreshToken,
889
- })
890
- .toPromise()
891
- .catch(async (error) => {
892
- if (error.error &&
893
- error.error.code &&
894
- error.error.code === 'session.refreshtoken.invalid') {
895
- await this.startSession();
882
+ try {
883
+ const sessionInfo = await lastValueFrom(this.apiService.refreshSession({ refreshToken }));
884
+ if (sessionInfo) {
885
+ this.updateSessionInfo(sessionInfo);
886
+ return sessionInfo;
896
887
  }
897
- });
898
- if (sessionInfo) {
899
- this.sessionInfoData = sessionInfo;
900
- this.token = sessionInfo.sid;
901
- this.saveTokenToLocalStorage();
902
- this.setAuthenticationStatus();
903
- this.setUpTokenForSessionService();
904
888
  }
905
- return this.sessionInfoData;
889
+ catch (error) {
890
+ if (this.isInvalidRefreshTokenError(error)) {
891
+ this.clearAndStartNewSession();
892
+ // still throw the error, so the error can be handled in the caller
893
+ }
894
+ throw error;
895
+ }
896
+ // no sessionInfo returned
897
+ throw new Error('Unable to refresh session');
898
+ }
899
+ updateSessionInfo(sessionInfo) {
900
+ this.sessionInfoData = sessionInfo;
901
+ this.token = sessionInfo.sid;
902
+ this.saveTokenToLocalStorage();
903
+ this.setAuthenticationStatus();
904
+ this.setUpTokenForSessionService();
905
+ }
906
+ isInvalidRefreshTokenError(error) {
907
+ return (error instanceof HttpErrorResponse &&
908
+ error.error &&
909
+ error.error.code === 'session.refreshtoken.invalid');
906
910
  }
907
911
  async getAuthenticationProviders() {
908
912
  let providers = await this.apiService
@@ -3286,52 +3290,68 @@ class SmartErrorCatchingInterceptor {
3286
3290
  this.session = session;
3287
3291
  this.sessionErrorCodes = [
3288
3292
  {
3289
- code: "session.expired",
3293
+ code: 'session.expired',
3290
3294
  behaviour: SessionErrorBehaviour.REFRESH,
3291
3295
  },
3292
3296
  {
3293
- code: "session.refreshtoken.invalid",
3297
+ code: 'session.refreshtoken.invalid',
3294
3298
  behaviour: SessionErrorBehaviour.RESTART,
3295
3299
  },
3296
3300
  {
3297
- code: "session.badrequest.missingtoken",
3301
+ code: 'session.badrequest.missingtoken',
3298
3302
  behaviour: SessionErrorBehaviour.RESTART,
3299
3303
  },
3300
3304
  {
3301
- code: "session.security.nocontext",
3305
+ code: 'session.security.nocontext',
3302
3306
  behaviour: SessionErrorBehaviour.RESTART,
3303
3307
  },
3304
3308
  {
3305
- code: "session.invalidsessionuri",
3309
+ code: 'session.invalidsessionuri',
3306
3310
  behaviour: SessionErrorBehaviour.RESTART,
3307
3311
  },
3308
3312
  {
3309
- code: "session.notinitialized",
3313
+ code: 'session.notinitialized',
3310
3314
  behaviour: SessionErrorBehaviour.RESTART,
3311
3315
  },
3312
3316
  {
3313
- code: "session.viewcontext.missing",
3317
+ code: 'session.viewcontext.missing',
3314
3318
  behaviour: SessionErrorBehaviour.RESTART,
3315
3319
  },
3316
3320
  ];
3317
3321
  }
3318
3322
  intercept(request, next) {
3319
- return next.handle(request).pipe(catchError$1((error) => {
3320
- if (error.status === 400 && error.error.code) {
3321
- let sessionError = this.sessionErrorCodes.find((sessionError) => sessionError.code === error.error.code);
3322
- if (sessionError) {
3323
- return this.resolveError(sessionError).pipe(switchMap(() => {
3324
- this.session.headerParams.map((headerParam) => {
3325
- request = request.clone({
3326
- headers: request.headers.set(headerParam.headerName, headerParam.value),
3327
- });
3328
- });
3329
- return next.handle(request);
3330
- }));
3331
- }
3323
+ return next
3324
+ .handle(request)
3325
+ .pipe(catchError$1((error) => this.handleError(error, request, next)));
3326
+ }
3327
+ handleError(error, request, next) {
3328
+ if (this.isSessionError(error)) {
3329
+ const sessionError = this.getSessionError(error);
3330
+ if (sessionError) {
3331
+ return this.resolveError(sessionError).pipe(switchMap((resolved) => {
3332
+ if (resolved && sessionError.behaviour === SessionErrorBehaviour.REFRESH) {
3333
+ return next.handle(this.updateRequestHeaders(request));
3334
+ }
3335
+ return throwError(() => error);
3336
+ }), catchError$1((resolveError) => {
3337
+ // when the resolveError throws a restart error (in the refreshSession), rethrow the original error
3338
+ if (this.isSessionError(resolveError)) {
3339
+ const resolveSessionError = this.getSessionError(resolveError);
3340
+ if (resolveSessionError?.behaviour === SessionErrorBehaviour.RESTART) {
3341
+ return throwError(() => error);
3342
+ }
3343
+ }
3344
+ return throwError(() => resolveError);
3345
+ }));
3332
3346
  }
3333
- return throwError(error);
3334
- }));
3347
+ }
3348
+ return throwError(() => error);
3349
+ }
3350
+ isSessionError(error) {
3351
+ return error.status === 400 && error.error.code != null;
3352
+ }
3353
+ getSessionError(error) {
3354
+ return this.sessionErrorCodes.find((sessionError) => sessionError.code === error.error.code);
3335
3355
  }
3336
3356
  resolveError(sessionError) {
3337
3357
  if (sessionError.behaviour === SessionErrorBehaviour.RESTART) {
@@ -3339,6 +3359,14 @@ class SmartErrorCatchingInterceptor {
3339
3359
  }
3340
3360
  return from(this.session.refreshSession());
3341
3361
  }
3362
+ updateRequestHeaders(request) {
3363
+ this.session.headerParams.forEach((headerParam) => {
3364
+ request = request.clone({
3365
+ headers: request.headers.set(headerParam.headerName, headerParam.value),
3366
+ });
3367
+ });
3368
+ return request;
3369
+ }
3342
3370
  }
3343
3371
  SmartErrorCatchingInterceptor.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: SmartErrorCatchingInterceptor, deps: [{ token: SmartSessionService }], target: i0.ɵɵFactoryTarget.Injectable });
3344
3372
  SmartErrorCatchingInterceptor.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.4.0", ngImport: i0, type: SmartErrorCatchingInterceptor });