fix: handle token refresh gracefully in auth interceptor and update loading interceptor to skip refresh token requests

This commit is contained in:
Bangara Raju Kottedi 2025-11-17 15:15:28 +05:30
parent e83e2b2161
commit 9e34de73ee
2 changed files with 24 additions and 4 deletions

View File

@ -73,14 +73,30 @@ export const AuthInterceptor: HttpInterceptorFn = (
refreshInProgress = true;
refreshSubject.next(null);
return authSvc.refreshToken()!.pipe(
const refresh$ = authSvc.refreshToken();
// refreshToken() must return an Observable. Otherwise handle gracefully
if (!refresh$) {
refreshInProgress = false;
authSvc.logout();
return throwError(() => err);
}
return refresh$.pipe(
switchMap(res => {
refreshInProgress = false;
const newToken = res.accessToken;
authSvc.safeSetToken(newToken);
const newToken = res?.accessToken;
if (!newToken) {
authSvc.logout();
return throwError(() => err);
}
authSvc.safeSetToken(newToken);
refreshSubject.next(newToken);
const retryReq = clonedRequest.clone({
headers: clonedRequest.headers.set('Authorization', `Bearer ${newToken}`),
withCredentials: true

View File

@ -12,6 +12,10 @@ export const loadingInterceptor: HttpInterceptorFn = (
const loadingSvc = inject(LoaderService);
if(req.url.includes('/RefreshToken')) {
return next(req);
}
totalRequests++;
loadingSvc.setLoading(true);