diff --git a/console/src/app/services/interceptors/auth.interceptor.ts b/console/src/app/services/interceptors/auth.interceptor.ts index f609398421..37b1371dc0 100644 --- a/console/src/app/services/interceptors/auth.interceptor.ts +++ b/console/src/app/services/interceptors/auth.interceptor.ts @@ -1,7 +1,8 @@ import { Injectable } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { Request, UnaryInterceptor, UnaryResponse } from 'grpc-web'; -import { filter, first, take } from 'rxjs/operators'; +import { Subject } from 'rxjs'; +import { debounceTime, filter, first, take } from 'rxjs/operators'; import { WarnDialogComponent } from 'src/app/modules/warn-dialog/warn-dialog.component'; import { AuthenticationService } from '../authentication.service'; @@ -16,11 +17,16 @@ const accessTokenStorageKey = 'access_token'; * Set the authentication token */ export class AuthInterceptor implements UnaryInterceptor { + public triggerDialog: Subject = new Subject(); constructor( private authenticationService: AuthenticationService, private storageService: StorageService, private dialog: MatDialog, - ) { } + ) { + this.triggerDialog.pipe(debounceTime(1000)).subscribe(() => { + this.openDialog(); + }); + } public async intercept(request: Request, invoker: any): Promise> { await this.authenticationService.authenticationChanged.pipe( @@ -36,22 +42,26 @@ export class AuthInterceptor implements UnaryIn return response; }).catch((error: any) => { if (error.code === 16) { - const dialogRef = this.dialog.open(WarnDialogComponent, { - data: { - confirmKey: 'ACTIONS.LOGIN', - titleKey: 'ERRORS.TOKENINVALID.TITLE', - descriptionKey: 'ERRORS.TOKENINVALID.DESCRIPTION', - }, - width: '400px', - }); - - dialogRef.afterClosed().pipe(take(1)).subscribe(resp => { - if (resp) { - this.authenticationService.authenticate(undefined, true, true); - } - }); + this.triggerDialog.next(true); } return Promise.reject(error); }); } + + openDialog() { + const dialogRef = this.dialog.open(WarnDialogComponent, { + data: { + confirmKey: 'ACTIONS.LOGIN', + titleKey: 'ERRORS.TOKENINVALID.TITLE', + descriptionKey: 'ERRORS.TOKENINVALID.DESCRIPTION', + }, + width: '400px', + }); + + dialogRef.afterClosed().pipe(take(1)).subscribe(resp => { + if (resp) { + this.authenticationService.authenticate(undefined, true, true); + } + }); + } }