-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2494 from aura-nw/baseline/main_20231009
Baseline/main 20231009
- Loading branch information
Showing
157 changed files
with
3,331 additions
and
2,667 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,54 @@ | ||
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http'; | ||
import { Observable, throwError } from 'rxjs'; | ||
import { catchError } from 'rxjs/operators'; | ||
import { AuthenticationService } from '../services/auth.service'; | ||
import { UserService } from '../services/user.service'; | ||
import { Router } from '@angular/router'; | ||
|
||
@Injectable() | ||
export class ErrorInterceptor implements HttpInterceptor { | ||
constructor(private userService: UserService, private router: Router) {} | ||
|
||
constructor(private authenticationService: AuthenticationService) { } | ||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
return next.handle(request).pipe( | ||
catchError((err) => { | ||
// check status unauthorized, refresh token | ||
if (err?.error?.statusCode === 401 && err?.error?.message == 'Unauthorized') { | ||
const payload = { | ||
refreshToken: localStorage.getItem('refreshToken').replace(/"/g, ''), | ||
}; | ||
this.userService.refreshToken(payload).subscribe({ | ||
next: (res) => { | ||
if (res.error?.statusCode === 400) { | ||
// redirect to log out | ||
localStorage.removeItem('accessToken'); | ||
localStorage.removeItem('refreshToken'); | ||
localStorage.removeItem('userEmail'); | ||
this.router.navigate(['/login']); | ||
|
||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
return next.handle(request).pipe(catchError(err => { | ||
if (err.status === 401) { | ||
// auto logout if 401 response returned from api | ||
// location.reload(); | ||
} | ||
const error = err.error || err.statusText; | ||
return throwError(error); | ||
})) | ||
} | ||
} | ||
setTimeout(() => { | ||
location.reload(); | ||
}, 500); | ||
} | ||
|
||
localStorage.setItem('accessToken', JSON.stringify(res.accessToken)); | ||
localStorage.setItem('refreshToken', JSON.stringify(res.refreshToken)); | ||
}, | ||
error: () => { | ||
localStorage.removeItem('accessToken'); | ||
localStorage.removeItem('refreshToken'); | ||
localStorage.removeItem('userEmail'); | ||
this.router.navigate(['/login']); | ||
|
||
setTimeout(() => { | ||
location.reload(); | ||
}, 500); | ||
}, | ||
}); | ||
} | ||
const error = err.error || err.statusText; | ||
return throwError(error); | ||
}), | ||
); | ||
} | ||
} |
Oops, something went wrong.