From 6fd981ca72425eee0dff2baccad3173be593366f Mon Sep 17 00:00:00 2001 From: andrej romanov <50377758+auumgn@users.noreply.github.com> Date: Fri, 14 Jun 2024 11:54:35 +0300 Subject: [PATCH 1/3] add actual interceptor for expired auths --- ui/src/app/error/service/error.service.ts | 21 +---------- .../interceptor/auth-expired.interceptor.ts | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 ui/src/app/shared/interceptor/auth-expired.interceptor.ts 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(['/']) + } + } + } + }, + }) + ) + } +} From 75c2a51abb80543c6d7aa4f7d2332a8640be0d1a Mon Sep 17 00:00:00 2001 From: andrej romanov <50377758+auumgn@users.noreply.github.com> Date: Fri, 14 Jun 2024 11:59:09 +0300 Subject: [PATCH 2/3] lint --- ui/src/app/shared/interceptor/auth-expired.interceptor.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ui/src/app/shared/interceptor/auth-expired.interceptor.ts b/ui/src/app/shared/interceptor/auth-expired.interceptor.ts index af5d393e7..127faf27d 100644 --- a/ui/src/app/shared/interceptor/auth-expired.interceptor.ts +++ b/ui/src/app/shared/interceptor/auth-expired.interceptor.ts @@ -17,7 +17,6 @@ export class AuthExpiredInterceptor implements HttpInterceptor { 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) { From 68a1fa149ecaefc95df2b1c6e0ef6d64e0681ecf Mon Sep 17 00:00:00 2001 From: andrej romanov <50377758+auumgn@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:28:05 +0300 Subject: [PATCH 3/3] add interceptor to app module --- ui/src/app/app.module.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ui/src/app/app.module.ts b/ui/src/app/app.module.ts index 442050540..38180139b 100644 --- a/ui/src/app/app.module.ts +++ b/ui/src/app/app.module.ts @@ -15,6 +15,7 @@ import { HeaderInterceptor } from './shared/interceptor/header.interceptor' import { ErrorService } from './error/service/error.service' import { ErrorComponent } from './error/error.component' import { FormsModule } from '@angular/forms' +import { AuthExpiredInterceptor } from './shared/interceptor/auth-expired.interceptor' @NgModule({ declarations: [AppComponent, NavbarComponent, FooterComponent, ErrorComponent], @@ -35,6 +36,11 @@ import { FormsModule } from '@angular/forms' useClass: HeaderInterceptor, multi: true, }, + { + provide: HTTP_INTERCEPTORS, + useClass: AuthExpiredInterceptor, + multi: true, + }, { provide: ErrorHandler, useClass: ErrorService,