Astrit Shuli
2 min readNov 4, 2023

--

Thank you for your insightful comment and question! In Angular, interceptors can be powerful tools, and understanding their interaction is crucial for handling scenarios like the one you described.

In your specific case, if you have both a JWT interceptor and a retry interceptor, the sequence in which they are applied is important. Interceptors are executed in the order in which they are added to the HTTP request pipeline. You have control over the sequence by the order in which you add them to your HttpClientModule.

To achieve the behavior you described, you should add the JWT interceptor first and the retry interceptor second. This way, when a 401 error occurs due to an expired access token, the JWT interceptor will be the first to intercept the request. It can try to refresh the token, and if successful, it will allow the request to proceed. If it fails to refresh the token, it can return an error response with an appropriate status code (e.g., 401). The retry interceptor, being the second in the sequence, can then kick in and attempt to retry the request up to three times or as configured.

In this way, the retry interceptor will operate with the knowledge of whether the JWT interceptor was able to refresh the token, ensuring that you don't retry requests with a known bad token.

So, yes, it makes perfect sense! Proper ordering of interceptors is essential for handling complex scenarios effectively. Make sure to configure your interceptors accordingly, and you'll achieve the behavior you're aiming for. If you have any more questions or need further clarification, please feel free to ask!

--

--

Astrit Shuli
Astrit Shuli

Written by Astrit Shuli

Full Stack Developer | Angular Specialist

No responses yet