Here is my code below...the problem is after the token is expired(401) the api for getting refresh token get hit, but the request is not re-sent with the inner next.handle().
export class InterceptService implements HttpInterceptor {
isRefreshingToken = false;
isUserLoggedIn$: Observable<boolean> = this.store.pipe(select(isLoggedIn));
constructor(public auth: AuthService, private store: Store<AppState>,http: HttpClient) {}
private addTokenHeader(request: HttpRequest<any>, token: string) {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${token})}`
}
});
return request;
}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `Bearer ${localStorage.getItem(environment.authTokenKey)}`
}
});
return next.handle(request).pipe(
tap(
event => {
if (event instanceof HttpResponse) {
}
},
error => {
if(error.status === 401 && !this.isRefreshingToken && this.isUserLoggedIn$) {
this.isRefreshingToken = true;
this.auth.getRefreshToken().subscribe( (authToken: IAuthToken) =>
{
this.store.dispatch(new TokenSet({authToken}));
let newReq = this.addTokenHeader(request, authToken.token);
return next.handle(newReq);
}
);
}
else {
this.isRefreshingToken = false;
}
}
)
);
}
}
But I want a re-sent of role api after refreshToken api.
CodePudding user response:
Take a look at this Stackblitz example which is fully working on my application at the moment.
You're missing the code which re-submits the failed request.