I'm working on an angular application where it has interaction with a js library. My objective is to be able to intercept these calls and pass an authorization bearer token in the request header for calls made by the js library.
I've seen examples of this being done in angularjs and angular 2 with the Http API, but can't figure out how to do the same with HttpClient.
So far I've tried to make an interceptor:
@Injectable()
export class BasicInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// All HTTP requests from Angular go through this. How would I get all AJAX requests to?
console.log("Intercepted Request", req);
return next.handle(req);
}
}
and provided it in my app.module.ts but haven't been able to figure how I would do something similar to this question as the API is different
The alternatives I have thought are: - Using localStorage between the angular application and library so the library could utilize this to make the API call. - After getting a token calling a function in the library to provide it the said token to use (would still need to rely on localStorage to persist for a session)