/ Web

ngx-admin infinite interceptor call

Problem

After setting up Auth in ngx-admin, the http call gets stuck in interceptor. You may see error in console like Max call stack reached...

Pattern

First step to solve a problem is finding when or what the problem is triggered.

The problem seems to happen daily and the workaround is to clear the token local storage. This gives a good hint that the cause is the invalid(expired) token. Following this lead, we can check the source code of AuthService.

Github source auth.service.ts

isAuthenticatedOrRefresh(): Observable<boolean> {
    return this.getToken()
      .pipe(
        switchMap(token => {
        if (token.getValue() && !token.isValid()) {
          return this.refreshToken(token.getOwnerStrategyName(), token)
            .pipe(
              switchMap(res => {
                if (res.isSuccess()) {
                  return this.isAuthenticated();
                } else {
                  return observableOf(false);
                }
              }),
            )
        } else {
          return observableOf(token.isValid());
        }
    }));
  }

Solution

With this, we clearly see that the refresh is causing the problem.

The interceptor tries to refresh the token, and the refresh request is intercepted again... Finally stack overflow.

So just filter out the authentication urls in your interceptor, problem solved!

mimimi

mimimi

Currently a Computer Engineering student at the National University of Singapore, passionate about software engineering. Contact me: info at mimimi.fun

Read More
ngx-admin infinite interceptor call
Share this