Setting global headers from a promise
I believe this is more of a JavaScript question than a angular2-jwt question but I'm hoping someone else may have had to do something similar.
In addition to my default bearer authorization header, I want to send another custom header to all my http requests. However the custom header function returns Promise<string> and not a string. What is the best way to resolve a promise before they're set?
// My basic setup
export function authHttpServiceFactory(http: Http, options: RequestOptions, authService: AuthService) {
return new AuthHttp(new AuthConfig({
tokenGetter: (() => { return authService.getIdToken(); }),
}), http, options);
}
// getCustomToken() returns a promise (I can't change it as it's an external library)
return authService.getCustomToken().then(customToken => {
return new AuthHttp(new AuthConfig({
globalHeaders: [{ 'custom-token': customToken }],
tokenGetter: (() => { return authService.getIdToken(); }),
}), http, options);
});
This makes the factory return a Promise<AuthHttp> which breaks my services as I can't just inject http: AuthHttp and use http.get(). What's the best practice to handle this kind of scenario?
I'm closing this. I found a solution here: https://github.com/auth0/angular2-jwt/issues/184#issuecomment-300763596
I ended up creating an interceptor that resolves the promise and updates headers before each request.