I have almost everything setup in my project with regards to codeswagger and I'm having trouble (due to my inexperience with Angular) injecting the access token gotten from my AuthService into the ConfigurationFactory.
At the moment I have my set up like this
...
imports: [
ApiModule.forConfig(()=> new Configuration({ basePath: process.env.apiLocation }),
],
...
Access tokens get set during run time through the "AuthorizationModule" and there is a provider for AuthService which has the user and the access token. Is there a recommended way to inject this token into the configuration factory? Any help would be appreciated.
Thank you
My first suggestion is to start with Swagger Codegen 2.3.0 branch as there's been many enhancements to the TS Angular 2 clients.
This is what I have,
On my AppModule:
providers: [
{
provide: Configuration,
useFactory: (authService: AuthService) => new Configuration({ basePath: process.env.apiLocation, accessToken: authService.GetAccessToken.bind(authService) }),
deps: [AuthService],
multi: false
}
],
The useFactory will be injected with your AuthService, given that you registered it as well. Angular looks up the required parameter type and searches in the providers list if you have that type registered, if so it injects the parameter. "deps" is short for dependencies, which tells it to go grab it from dependency injection. "multi: false" means it's only going to create one instance of Configuration.
Also import the ApiModule without doing the forConfig() thing. so like this:
...
imports: [
ApiModule
],
...
Dependency injection will take care of injecting the Configuration
And I have this function that returns the access token in the AuthService. The Module basically binds to this function and invokes it to get the AccessToken when needed.
public GetAccessToken(): string { return this.CurrentUser ? this.CurrentUser.access_token : ''; }
cc @Vrolijkx @taxpon @sebastianhaas @kenisteward @TiFu
Is there still an open question @BicakciHasan?
Yes, thank you @kewur that helped.
I use @kewur's answer combined with a subscription on the current user. So, in the useFactory, I subscribe to a BehaviorSubject that emits any current user changes and update the config accordingly.
@kewur thank you!
I use @kewur's answer combined with a subscription on the current user. So, in the useFactory, I subscribe to a BehaviorSubject that emits any current user changes and update the config accordingly.
@nikojpapa could you please elaborate the solution of how to handle this case properly?
@dgensert You can create a BehaviorSubject that contains the user information
currentUser$ = new BehaviorSubject<UserInfoOps>(this.currentUser);. Then you can update it with this.currentUser$.next(user); at any time. In your useFactory function, as described by @kewur, subscribe to the BehaviorSubject and make any necessary changes to the configuration.
export function apiConfig(sessionService: SessionService): Configuration {
const conf = new Configuration({
basePath: `${window.location.origin}`
});
sessionService.currentUser$.subscribe(
currentUser => {
if (currentUser) {
const token = currentUser.token;
const username = currentUser.username;
}
...
);
return conf;
}
The sessonService is imported with the deps property, as described by @kewur. This way, you're always using the persistent object for the config and just modifying it on user updates.
Most helpful comment
This is what I have,
On my AppModule:
providers: [ { provide: Configuration, useFactory: (authService: AuthService) => new Configuration({ basePath: process.env.apiLocation, accessToken: authService.GetAccessToken.bind(authService) }), deps: [AuthService], multi: false } ],The useFactory will be injected with your AuthService, given that you registered it as well. Angular looks up the required parameter type and searches in the providers list if you have that type registered, if so it injects the parameter. "deps" is short for dependencies, which tells it to go grab it from dependency injection. "multi: false" means it's only going to create one instance of Configuration.
Also import the ApiModule without doing the forConfig() thing. so like this:
... imports: [ ApiModule ], ...Dependency injection will take care of injecting the Configuration
And I have this function that returns the access token in the AuthService. The Module basically binds to this function and invokes it to get the AccessToken when needed.
public GetAccessToken(): string { return this.CurrentUser ? this.CurrentUser.access_token : ''; }