2020-11-16 14:36:12 +01:00
|
|
|
import {Injectable} from "@angular/core";
|
|
|
|
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
|
|
|
|
import {Observable, throwError} from "rxjs";
|
|
|
|
import {catchError} from "rxjs/operators";
|
|
|
|
import {Session} from "./login/utils/helper.class";
|
|
|
|
import {Router} from "@angular/router";
|
|
|
|
import {LoginErrorCodes} from "./login/utils/guardHelper.class";
|
|
|
|
import {properties} from "../../environments/environment";
|
2020-11-25 17:45:41 +01:00
|
|
|
import {isArray} from "util";
|
2020-11-16 14:36:12 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ErrorInterceptorService implements HttpInterceptor {
|
|
|
|
|
2020-11-25 17:45:41 +01:00
|
|
|
private static UNAUTHORIZED_WHITELIST = [properties.userInfoUrl];
|
|
|
|
|
2020-11-16 14:36:12 +01:00
|
|
|
constructor(private router: Router) {
|
|
|
|
}
|
|
|
|
|
|
|
|
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
|
|
|
return next.handle(req).pipe(catchError(err => {
|
2020-11-25 17:45:41 +01:00
|
|
|
if ((err.status === 0 && this.isService(req, properties.registryUrl)) ||
|
2020-11-25 18:32:20 +01:00
|
|
|
(err.status === 401 && !this.isService(req, ErrorInterceptorService.UNAUTHORIZED_WHITELIST))) {
|
2020-11-16 14:36:12 +01:00
|
|
|
this.logOut();
|
2020-11-25 18:32:20 +01:00
|
|
|
} else if(err.status === 403) {
|
|
|
|
this.unauthorized();
|
2020-11-16 14:36:12 +01:00
|
|
|
}
|
|
|
|
return throwError(err);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-11-25 17:45:41 +01:00
|
|
|
isService(req: HttpRequest<any>, service: string | string[]):boolean {
|
|
|
|
if(isArray(service)) {
|
|
|
|
return !!service.find(element => req.url.indexOf(element) !== -1);
|
|
|
|
} else {
|
|
|
|
return req.url.indexOf(service) !== -1;
|
|
|
|
}
|
2020-11-16 14:36:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logOut() {
|
|
|
|
Session.removeUser();
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': LoginErrorCodes.NOT_LOGIN,
|
|
|
|
'redirectUrl': this.router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-25 18:32:20 +01:00
|
|
|
|
|
|
|
unauthorized() {
|
|
|
|
this.router.navigate(['/user-info'], {
|
|
|
|
queryParams: {
|
|
|
|
'errorCode': LoginErrorCodes.NOT_AUTHORIZED,
|
|
|
|
'redirectUrl': this.router.url
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-11-16 14:36:12 +01:00
|
|
|
}
|