angular-auth0-aside
angular-auth0-aside copied to clipboard
HttpInterceptor should take the first token then unsubscribe
The InterceptorService currently (AFAICT) creates a new subscription for each http request and never unsubscribes - this causes 3 issues:
- Memory leak
- If the token ever gets refreshed all of the previous http requests will be repeated
- It's impossible to chain requests because the observable never completes
Adding a call to first after the filter resolves this (and changing the BehaviorSubjects to ReplaySubjects in the Auth Service removes the need for the filter).
return this.auth.token$
.pipe(
filter(token => typeof token === 'string'),
first(),
mergeMap(token => {
const tokenReq = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
return next.handle(tokenReq);
})
);
@jebbench good job!