I am facing an issue with the switchMap in interceptor. But in angular service it works fine. unable to find the reason why switchMap is not executing its code. Need help please.
getAccessToken
getAccessToken(): Observable<string> {
const headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });
const body = new HttpParams()
.set('client_id', AppConfig.settings.ClientId)
.set('client_secret', AppConfig.settings.ClientSecret);
return this.http.post<{ access_token: string }>(AppConfig.settings.tokenURL, body.toString(), { headers }).pipe( map(response => response.access_token) );
}
Interceptor:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.authService.getAccessToken().pipe(
switchMap(token => {
const headers = new HttpHeaders({
'Authorization': `Bearer ${token}`,
'Accept': 'application/json'
});
const clonedRequest = req.clone({ headers });
return next.handle(clonedRequest);
})
);
}