diff --git a/ui/src/app/error/service/error.service.ts b/ui/src/app/error/service/error.service.ts index 69304cf58..0fcd69f66 100644 --- a/ui/src/app/error/service/error.service.ts +++ b/ui/src/app/error/service/error.service.ts @@ -2,8 +2,6 @@ import { HttpErrorResponse } from '@angular/common/http' import { ErrorHandler, Injectable } from '@angular/core' import { Observable, Subject } from 'rxjs' import { AppError } from '../model/error.model' -import { Router } from '@angular/router' -import { LoginService } from 'src/app/account' // To inject this service, you have to include '@Inject(ErrorHandler)' to be able to subscribe to observables, e.g.: // @Inject(ErrorHandler) private errorService: ErrorService @@ -11,26 +9,11 @@ import { LoginService } from 'src/app/account' @Injectable({ providedIn: 'root' }) export class ErrorService implements ErrorHandler { private errors: Subject = new Subject() - NON_CHECKED_URLS = ['/', '/reset/request', '/reset/finish'] - constructor( - private router: Router, - private loginService: LoginService - ) {} handleError(error: any) { console.log(error) - if (error instanceof HttpErrorResponse || error.name === 'HttpErrorResponse') { - if (error.status === 401) { - if (this.loginService.isAuthenticated()) { - this.loginService.logoutDirectly() - this.router.navigate(['/']) - } else if (!this.NON_CHECKED_URLS.find((x) => this.router.url.startsWith(x))) { - this.loginService.logout() - this.router.navigate(['/']) - } - } else { - this.errors.next(new AppError(error.status, error.error.title || error.message)) - } + if (error instanceof HttpErrorResponse) { + this.errors.next(new AppError(error.status, error.error.title || error.message)) } else { console.error('Unknown error occurred', error) } diff --git a/ui/src/app/shared/interceptor/auth-expired.interceptor.ts b/ui/src/app/shared/interceptor/auth-expired.interceptor.ts new file mode 100644 index 000000000..af5d393e7 --- /dev/null +++ b/ui/src/app/shared/interceptor/auth-expired.interceptor.ts @@ -0,0 +1,37 @@ +import { Injectable } from '@angular/core' +import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http' +import { Observable } from 'rxjs' +import { tap } from 'rxjs/operators' +import { Router } from '@angular/router' +import { LoginService } from 'src/app/account' + +@Injectable() +export class AuthExpiredInterceptor implements HttpInterceptor { + NON_CHECKED_URLS = ['/', '/reset/request', '/reset/finish'] + + constructor( + private router: Router, + private loginService: LoginService + ) {} + + intercept(request: HttpRequest, next: HttpHandler): Observable> { + return next.handle(request).pipe( + tap({ + next: (event: HttpEvent) => {}, + error: (err: any) => { + if (err instanceof HttpErrorResponse) { + if (err.status === 401) { + if (this.loginService.isAuthenticated()) { + this.loginService.logoutDirectly() + this.router.navigate(['/']) + } else if (!this.NON_CHECKED_URLS.find((x) => this.router.url.startsWith(x))) { + this.loginService.logout() + this.router.navigate(['/']) + } + } + } + }, + }) + ) + } +}