Skip to content

Commit

Permalink
add actual interceptor for expired auths
Browse files Browse the repository at this point in the history
  • Loading branch information
auumgn committed Jun 14, 2024
1 parent c6b5980 commit 6fd981c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
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
37 changes: 37 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,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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap({
next: (event: HttpEvent<any>) => {},

Check failure on line 20 in ui/src/app/shared/interceptor/auth-expired.interceptor.ts

View workflow job for this annotation

GitHub Actions / format

Unexpected empty method 'next'
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 6fd981c

Please sign in to comment.