In my angular 4 application, I use ngx-restangularto handle all server calls. It returns the observed result, and this module has hooks for handling errors (e.g. 401, etc.).
But from the documentation, I can handle 403 (401), so:
RestangularProvider.addErrorInterceptor((response, subject, responseHandler) => {
if (response.status === 403) {
refreshAccesstoken()
.switchMap(refreshAccesstokenResponse => {
response.request.headers.set('Authorization', 'Bearer ' + refreshAccesstokenResponse)
return response.repeatRequest(response.request);
})
.subscribe(
res => responseHandler(res),
err => subject.error(err)
);
return false;
}
return true;
});
and this is good for one request that failed with error 403. how can I stack these calls using rxJs? Now I have 3 requests that have 403, and for each of this broken request I update the token - this is not so good, I need to update the token and then repeat all my broken requests. How can I achieve this with Observables?
In angular 1, this was pretty simple:
Restangular.setErrorInterceptor(function (response, deferred, responseHandler) {
if (response.status == 403) {
if (!refreshingIsInProgress) {
refreshingIsInProgress = AppApi.refreshAccessToken();
}
$q.when(refreshingIsInProgress, function () {
refreshingIsInProgress = null;
setHeaders(response.config.headers);
$http(response.config).then(responseHandler, deferred);
}, function () {
refreshingIsInProgress = null;
$state.go('auth');
});
return false;
}
return true;
});
. rxJs angular 4, , angular 4. , - ?
!
refreshAccesstoken
const refreshAccesstoken = function () {
const refreshToken = http.post(environment.apiURL + `/token/refresh`,
{refreshToken: 'someToken'});
return refreshToken;
};