Hello, every one .
I am implementing my refresh token using this post :
I’ve modified and my interceptor is :
intercept(req: HttpRequest, next: HttpHandler): Observable<HttpEvent> {
console.log(“InterceptorProvider”);
if (!req.url.includes(‘auth’)) {
let getTsObs = Observable.fromPromise(this.storage.get(‘TOKEN’));const tokenObservable = getTsObs.map(token => { return req = req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }); }); return tokenObservable.flatMap((req) => { console.log("FLATMAP"); return next.handle(req) .do((event: HttpEvent<any>) => { if (event instanceof HttpResponse) { // do stuff to the response here console.log("DOOO event " + this.typeRegister); } }, error => { if (error.status === 403) { alert("Error 403"); let obsGetEmail = Observable.fromPromise(this.storage.get('email')); let obsGetPass = Observable.fromPromise(this.storage.get('pass')); let email,pass; return obsGetEmail.flatMap(email => { email=email; return obsGetPass }).flatMap(pass => { pass=pass; return this._up.loginEmailObs(email, pass) }).mergeMap(refreshedToken => { let newToken = refreshedToken.result['access_token']; console.log("NEW TOKEN" + newToken); let clone = req.clone({ setHeaders: { Accept: `application/json`, 'Content-Type': `application/json`, Authorization: `Bearer ${newToken}` } }); return next.handle(clone); });
} else if (error.status === 200) {
console.log(“error.status”);
//return Observable.of(response.data);
return Observable.of(event);
} else {
console.log(“error ELSE”);
return Observable.of(event);
//return Observable.throw(error);
}
})
})
}
else {
console.log(“ES AUTH”);
let clone = req.clone({
setHeaders: {
Accept:application/json
,
‘Content-Type’:application/json
}
});
return next.handle(clone);
}
}
Could someone help me to achieve that ?