In an angular interceptor, I want to check wether an auth-token exists. If not, it should be refreshed and the request should be resent.
return next.handle(authReq).pipe(map((result: any) => {
if (result.body && result.body.error) {
if (result.body.error === 'ERR_TOKEN_EXPIRED' || result.body.error === 'ERR_TOKENS_DO_NOT_MATCH') {
console.log('Token is expired or invalid, refreshing.', result.body.error);
return this.userService.refreshLoginToken().subscribe(success => {
if (success) {
return this.intercept(req, next);
}
});
}
}
return result;
}));
The Problem is that I don't know how to replace the original Observable returned by next.handle() with a new one. The return statement before this.userService.refreshLoginToken().subscribe()
returns a Subscription object. If I just pipe the result of refreshLoginToken()
it wont work because refreshLoginToken sends an httprequest which is only executed when there is a subscription.
To reduce the question to a single line:
How can I replace the Observable returned in line 1 by next.handle() with the one returned by this.intercept(req, next)
?
Thank you!