From c396fd944ce21414af189ed843d56bc2c549e4ad Mon Sep 17 00:00:00 2001 From: Silke Date: Wed, 12 Jul 2023 15:33:06 +0200 Subject: [PATCH] fix: reload the application after an error occurred due to a changed pgid --- src/app/core/core.module.ts | 2 ++ .../interceptors/pgid-change.interceptor.ts | 25 +++++++++++++++++++ .../core/utils/api-token/api-token.service.ts | 4 ++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/app/core/interceptors/pgid-change.interceptor.ts diff --git a/src/app/core/core.module.ts b/src/app/core/core.module.ts index a8c30cdaf9..62416b3801 100644 --- a/src/app/core/core.module.ts +++ b/src/app/core/core.module.ts @@ -12,6 +12,7 @@ import { ICMErrorMapperInterceptor } from './interceptors/icm-error-mapper.inter import { IdentityProviderInterceptor } from './interceptors/identity-provider.interceptor'; import { MockInterceptor } from './interceptors/mock.interceptor'; import { PaymentPayoneInterceptor } from './interceptors/payment-payone.interceptor'; +import { PGIDChangeInterceptor } from './interceptors/pgid-change.interceptor'; import { PreviewInterceptor } from './interceptors/preview.interceptor'; import { InternationalizationModule } from './internationalization.module'; import { StateManagementModule } from './state-management.module'; @@ -29,6 +30,7 @@ import { DefaultErrorHandler } from './utils/default-error-handler'; StateManagementModule, ], providers: [ + { provide: HTTP_INTERCEPTORS, useClass: PGIDChangeInterceptor, multi: true }, { provide: HTTP_INTERCEPTORS, useClass: ICMErrorMapperInterceptor, multi: true }, { provide: HTTP_INTERCEPTORS, diff --git a/src/app/core/interceptors/pgid-change.interceptor.ts b/src/app/core/interceptors/pgid-change.interceptor.ts new file mode 100644 index 0000000000..c528602c6d --- /dev/null +++ b/src/app/core/interceptors/pgid-change.interceptor.ts @@ -0,0 +1,25 @@ +import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Observable, catchError, throwError } from 'rxjs'; + +/* eslint-disable @typescript-eslint/ban-types */ + +/** + * reload page when the pgid of the user has changed + */ +@Injectable() +export class PGIDChangeInterceptor implements HttpInterceptor { + intercept(req: HttpRequest, next: HttpHandler): Observable> { + return next.handle(req).pipe( + catchError((error: HttpErrorResponse) => { + if ( + error.name === 'HttpErrorResponse' && + error.message === 'Bad Request (The provided matrix parameter "pgid" does not match the authenticated user.)' + ) { + return throwError(() => window.location.reload()); + } + return throwError(() => error); + }) + ); + } +} diff --git a/src/app/core/utils/api-token/api-token.service.ts b/src/app/core/utils/api-token/api-token.service.ts index 637de8fdb2..5e6abcdc35 100644 --- a/src/app/core/utils/api-token/api-token.service.ts +++ b/src/app/core/utils/api-token/api-token.service.ts @@ -271,7 +271,9 @@ export class ApiTokenService { private isAuthTokenError(err: unknown) { return ( - err instanceof HttpErrorResponse && typeof err.error === 'string' && err.error?.toLowerCase().includes('token') + err instanceof HttpErrorResponse && + typeof err.error === 'string' && + (err.error.includes('AuthenticationToken') || err.error.includes('Unable to decode token')) ); }