angular-auth0-aside icon indicating copy to clipboard operation
angular-auth0-aside copied to clipboard

HttpInterceptor should take the first token then unsubscribe

Open jebbench opened this issue 6 years ago • 1 comments

The InterceptorService currently (AFAICT) creates a new subscription for each http request and never unsubscribes - this causes 3 issues:

  1. Memory leak
  2. If the token ever gets refreshed all of the previous http requests will be repeated
  3. 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 avatar Apr 01 '19 10:04 jebbench

@jebbench good job!

ABerzhanin avatar Jun 24 '19 09:06 ABerzhanin