Hello everyone .
I am using interceptors on my app, and I’ve followed this tutorial : Authorisation Headers with Ionic using HTTP Interceptor and Storage [v3] | Ionic Academy | Learn IonicIonic Academy | Learn Ionic
I’ve done some modification for it only function when error is 403( its my error when token is expired)
intercept(request: HttpRequest, next: HttpHandler): Observable<HttpEvent> {
let promise = this.storage.get('token'); return Observable.fromPromise(promise) .mergeMap(token => { let clonedReq = this.addToken(request, token); return next.handle(clonedReq).pipe( catchError(error => { // Perhaps display an error for specific status codes here already? if (error.status === 403) { // not authorized error .. do something let msg = error.message; let alert = this.alertCtrl.create({ title: error.name, message: msg, buttons: ['OK'] }); alert.present(); } // Pass the error to the caller of the function // return Observable.throw(error); //return _throw(error); return Observable.of(error); }) ); });
}
// Adds the token to your headers if it exists
private addToken(request: HttpRequest, token: any) {
if (token) {
let clone: HttpRequest;
clone = request.clone({
setHeaders: {
Accept:application/json
,
‘Content-Type’:application/json
,
Authorization:Bearer ${token}
}
});
return clone;
}
return request;
}
The context is : My component is requesting simultaneously 3 http requests, and they are catched by the interceptor’s catch block (which has an alert). I’m watching 3 alerts, but I want to get just 1 and ignore the others because The first one will be replaced to return the login page and remove storage.
private cleanApp() {
this._us.removeStorageTokenExpired();
let nav = this.app.getActiveNav();
nav.setRoot(‘Login’);
}
How can I get that , thank you in advance.