I implemented the Angular SPA quickstart in a Angular 9 app (https://auth0.com/docs/quickstart/spa/angular2/02-calling-an-api#create-an-http-interceptor)
The interceptor correctly sets the Authorization header for all HTTP method types.
However, for POST types, the req.body values for all keys are set to null in the this.auth.getTokenSilently$().pipe( ...
method
If I console.log the req.body before the this.auth.getTokenSilently$().pipe( ...
method, the req.body has the expected values.
Can anyone shed some light on why this issue is occurring?
(the console.log in the screengrabs incorrectly mention switchMap as I also tried switchMap, it should have read mergeMap)
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor
} from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable, throwError } from 'rxjs';
import { mergeMap, catchError, map, concatMap, switchMap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class InterceptorService implements HttpInterceptor {
constructor(private auth: AuthService) { }
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
console.log('pre switchmap req.body', req.body);
return this.auth.getTokenSilently$().pipe(
mergeMap(token => {
console.log('switchMap req.body', req.body);
const tokenReq = req.clone({
setHeaders: { Authorization: `Bearer ${token}` }
});
return next.handle(tokenReq);
}),
catchError(err => throwError(err))
);
}
}