How to handle cloned request with new headers?
I am trying to perform a cloned request in the new interceptors with the new token generated after the first request fail!
- If err 401 -> refresh token
- send back a copy of the previous request with the new headers
Here what I tried:
refreshSessionToken() {
return this.http.post('/connect', {})
.map(res => {
const token = res.json();
localStorage.setItem('token', token);
return token;
});
}
// Get the headers
const headers = getAuthenticationHeaders();
const reqMain = req.clone({headers: headers});
return next.handle(reqMain).catch(err => {
this.refreshSessionToken()
.subscribe(token => {
const t = token.token_id;
const clonedReq = req.clone({headers: req.headers.set('access-token', t)});
return next.handle(clonedReq);
});
})
getting a log of the clonedReq I can see that the token is refreshed, but the request (clonedReq) inside the subscribe is not performed, why?!
I tried other approaches on how to refresh the JWT but it doesn't seem to work in my case, any help on how to deal with it?
Thanks!
What do I expect to be my result?
- Send 1# HTTP request
- 1# request fail
- Refresh token (get/set token)
- Clone previous request and add refreshed token
- Send 2# HTTP request
- 2# request success
Similiar issues: