Merge pull request from votdev/improve_auth_interceptor

mgr/dashboard: Improve auth interceptor.

Reviewed-by: Ricardo Marques <rimarques@suse.com>
Reviewed-by: Stephan Müller <smueller@suse.com>
Reviewed-by: Tiago Melo <tmelo@suse.com>
This commit is contained in:
Ricardo Dias 2018-03-22 11:20:57 +00:00 committed by GitHub
commit fee6bcef3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,12 @@
import {
HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest,
HttpResponse
HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { ToastsManager } from 'ng2-toastr';
import 'rxjs/add/operator/do';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { Observable } from 'rxjs/Observable';
import { AuthStorageService } from './auth-storage.service';
@ -20,23 +20,24 @@ export class AuthInterceptorService implements HttpInterceptor {
}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do nothing
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 404) {
this.router.navigate(['/404']);
return;
return next.handle(request)
.catch((resp) => {
if (resp instanceof HttpErrorResponse) {
switch (resp.status) {
case 404:
this.router.navigate(['/404']);
break;
case 401:
this.authStorageService.remove();
this.router.navigate(['/login']);
// falls through
default:
this.toastr.error(resp.error.detail || '',
`${resp.status} - ${resp.statusText}`);
}
}
this.toastr.error(err.error.detail || '', `${err.status} - ${err.statusText}`);
if (err.status === 401) {
this.authStorageService.remove();
this.router.navigate(['/login']);
}
}
});
// Return the error to the method that called it.
return Observable.throw(resp);
});
}
}