argos/dmp-frontend/src/app/services/interceptor.ts

31 lines
999 B
TypeScript
Raw Normal View History

2017-10-27 10:20:35 +02:00
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http';
2017-11-02 09:33:30 +01:00
import {Router} from '@angular/router';
2017-10-27 10:20:35 +02:00
@Injectable()
export class GlobalInterceptor implements HttpInterceptor {
2017-12-18 16:55:12 +01:00
constructor( private router : Router) {}
2017-10-27 10:20:35 +02:00
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).do((event: HttpEvent<any>) => {
/*
if (event instanceof HttpResponse) {
console.log("response ok");
}
*/
}, (err: any) => {
2017-11-01 18:18:27 +01:00
if (err instanceof HttpErrorResponse) {
2017-10-27 10:20:35 +02:00
if (err.status === 401) {
2017-11-02 15:19:12 +01:00
console.log("Received an unauthorized... redirecting to login page");
2017-11-02 10:11:29 +01:00
this.router.navigate(['/login'], { queryParams: { /*returnUrl: this.state.url*/ }});
2017-10-27 10:20:35 +02:00
}
}
});
2017-11-01 18:18:27 +01:00
//return next.handle(req);
2017-10-27 10:20:35 +02:00
}
}