I'm trying to solve a problem related to refresh tokens. So basically, I have function that checks if somebody is logged in:
isLogged(): Observable<any> {
const valid = isTokenValid();
if (valid) return Observable.of(true);
else { return this.refresh() }
}
Obviously, this function returns observable. Now I have refresh function:
refresh() {
if (this.existingSubscription) return this.existingSubscription;
this.existingSubscription = this.http.post(...
return this.existingSubscription;
}
This generally works, BUT, this.existingSubscription.subscribe still executes request, so I get execution of 2 requests instead of 1. (1 promise, multiple subscribers).
E.g. When token is not valid and I send 5 requests at the same time, it sends 5 same request token requests instead of 1.
this.existingsSubscription.subscribe is executing code all over again instead of subscribing to the existing request that has already been made.
Am I doing something wrong?