From 96c00a1e020628e07ddadf4d4a1573529bae2b23 Mon Sep 17 00:00:00 2001 From: andrej romanov <50377758+auumgn@users.noreply.github.com> Date: Wed, 12 Jun 2024 18:18:23 +0300 Subject: [PATCH] navigate users to home page on 401 responses --- ui/src/app/error/service/error.service.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ui/src/app/error/service/error.service.ts b/ui/src/app/error/service/error.service.ts index a00ce914c..f0192b737 100644 --- a/ui/src/app/error/service/error.service.ts +++ b/ui/src/app/error/service/error.service.ts @@ -2,6 +2,8 @@ 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 @@ -9,12 +11,26 @@ import { AppError } from '../model/error.model' @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) { - this.errors.next(new AppError(error.status, error.error.title || error.message)) + 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)) + } } else { console.error('Unknown error occurred', error) }