I followed the spa angular tutorial to authenticate my user, all works fine but i dont want to use the HttpInterceptor, instead i want to inject the access token myself when needed.
So I try something like this :
get(atUrl: string): Observable {
return this.authService.getTokenSilently$().pipe(
concatMap(token => {
let headers = this.buildHTTPHeader(token);
let url = myurl;
return this.http.get(url, {
headers: headers
});
}),
catchError(err => throwError(err))
);
Can you confirm that the best method to manage token manually in httpClient ?
Yes, attaching your token to the request this way ought to be totally fine (assuming your headers also include the content-type).
The reason we’d use the interceptor is because it’s often a much simpler way to append a token to an outgoing request. Your solution has the tradeoff of needing to explicitly define headers each request.
A guard clause could also be placed in the interceptor logic to control when headers are applied based on the HttpRequest meeting certain conditions.