I am trying to implement update tokens in an Angular2 application. I use an optimistic approach, and instead of checking whether the access token lasted before executing the request, I make a request, and if it returns the code 401, I will update the access token by requesting a new one and save it to local storage,
Here is my code snippet:
getWithParams<T>(serviceUrl: string, params: URLSearchParams): Observable<T> {
return super.getWithParams<T>(serviceUrl, params)
.retryWhen((error) => {
return error
.filter((e) => e.status === 401)
.scan((acc, value) => {
return acc + 1;
}, 0)
.takeWhile(acc => acc < 3)
.flatMap(() => this.tokenRefreshService.refreshToken())
.delay(1000);
});
}
It is important to note that super.getWithParams sets an access token in the request headers, retrieving it from local storage.
The tokenRefreshService.refreshToken () method call gets a new access token and stores it in local storage.
, , , , super.getWithParams . .
? chainging , ?