Skip to content

Commit

Permalink
Merge pull request #1225 from ORCID/add-expired-authorization-interce…
Browse files Browse the repository at this point in the history
…ptor

navigate users to the home page on 401 responses
  • Loading branch information
bobcaprice authored Jun 12, 2024
2 parents 19269f7 + 96c00a1 commit d358e0b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ui/src/app/error/service/error.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@ 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

@Injectable({ providedIn: 'root' })
export class ErrorService implements ErrorHandler {
private errors: Subject<any> = new Subject<any>()
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)
}
Expand Down

0 comments on commit d358e0b

Please sign in to comment.