I'm implementing a JWT system in my Angular app, and in this case, in an Http Interceptor.
My "issue" is that the code below :
request = request.clone({
        setHeaders: {
          Authorization: `Bearer ${token}`
        }
      });
is trigerred before :
this.authService.refreshToken(token_refresh).subscribe(
            response => {
              localStorage.setItem('token', response.token);
              token = response.token;
            }
          );
Which is a normal behavior due to the asynchronous nature of the subscribe function, but in my case, I need the token in the setHeaders to be up to date, because is takes the older token which is invalid at this point.
I tried to make refreshtoken() return a promise with async await, but it didn't change anything.
Here's the whole code block :
 let token = localStorage.getItem('token');
 // if token is not valid or not set
  if ((this.jwtHelper.isTokenExpired(token) || token === null)) {
    if (token_refresh !== null) {
      // if token_refresh exists and is valid
      this.authService.refreshToken(token_refresh).subscribe(
        response => {
          localStorage.setItem('token', response.token);
          token = response.token;
        }
      );
    } else {
      // if no token_refresh exists or is expired
      this.router.navigate(['/register']);
    }
  }
  // always executed
  request = request.clone({
    setHeaders: {
      Authorization: `Bearer ${token}`
    }
  });
 
    