[Library | Trunk]: Add unauthorized whitelist to avoid logout in certain calls

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59965 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2020-11-25 16:45:41 +00:00
parent 3dfe33b5c7
commit f2ff8cfe8a
2 changed files with 18 additions and 4 deletions

View File

@ -6,24 +6,33 @@ import {Session} from "./login/utils/helper.class";
import {Router} from "@angular/router";
import {LoginErrorCodes} from "./login/utils/guardHelper.class";
import {properties} from "../../environments/environment";
import {isArray} from "util";
import {element} from "@angular/core/src/render3";
@Injectable()
export class ErrorInterceptorService implements HttpInterceptor {
private static UNAUTHORIZED_WHITELIST = [properties.userInfoUrl];
constructor(private router: Router) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(catchError(err => {
if ((err.status === 0 && this.isRegistryService(req)) || err.status === 401 || err.status === 403) {
if ((err.status === 0 && this.isService(req, properties.registryUrl)) ||
(err.status === 401 && !this.isService(req, ErrorInterceptorService.UNAUTHORIZED_WHITELIST)) || err.status === 403) {
this.logOut();
}
return throwError(err);
}));
}
isRegistryService(req: HttpRequest<any>):boolean {
return req.url.indexOf(properties.registryUrl) !== -1;
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;
}
}
logOut() {

View File

@ -62,7 +62,12 @@ export class UserManagementService{
if(resolve) {
resolve();
}
});
}, error => {
this.getUserInfoSubject.next(null);
if(resolve) {
resolve();
}
});
}
}