Concurrent token refresh triggers in HttpInterceptors
0 reputation · 24 May 2021, 16:46 UTC
When implementing least-privilege access in Angular, HttpInterceptors are typically used to handle 401 Unauthorized responses and manage token expiration. However, an issue arises when multiple parallel HTTP requests fail simultaneously due to an expired JWT-based access token.
The challenge lies in preventing the interceptor from initiating multiple token refresh calls. Without a synchronization mechanism, each intercepted request may trigger its own refresh logic, hitting the authentication provider unnecessarily and potentially leading to race conditions or invalid token sequences if the server only honors the most recent refresh token.
What is the recommended pattern using RxJS to queue concurrent failed requests while a single refresh operation completes? How can the queued requests be re-played automatically once the new token is successfully retrieved?