Skip to content

Commit

Permalink
Merge pull request #1228 from ORCID/add-actual-intercept-for-expired-…
Browse files Browse the repository at this point in the history
…authorizations

add actual interceptor for expired auths
  • Loading branch information
bobcaprice authored Jun 14, 2024
2 parents c6b5980 + 68a1fa1 commit 289017f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
6 changes: 6 additions & 0 deletions ui/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -35,6 +36,11 @@ import { FormsModule } from '@angular/forms'
useClass: HeaderInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthExpiredInterceptor,
multi: true,
},
{
provide: ErrorHandler,
useClass: ErrorService,
Expand Down
21 changes: 2 additions & 19 deletions ui/src/app/error/service/error.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,18 @@ 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 || 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)
}
Expand Down
36 changes: 36 additions & 0 deletions ui/src/app/shared/interceptor/auth-expired.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap({
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(['/'])
}
}
}
},
})
)
}
}

0 comments on commit 289017f

Please sign in to comment.