5

I am building a wrapper library 'auth-lib' for some existing(and out of my control) authentication libraries(for example keycloak-angular ). The authentication libraries I'm wrapping need to get initialized with configuration parameters during app initialization or bootstrapping which works when hard coding the configurations inside my Wrapper library but I can't get it to work with injection or forRoot from a sample application for my wrapper library.

How to pass the configuration parameters from my sample application through my wrapper library to the authentication libraries?

1 Answers1

15

You can pass the configuration from App.Module using forRoot to your Library Module.

Try like this:

App.module.ts

 imports: [
      BrowserModule,
      AuthLibModule.forRoot({
        configuration: {configA : "abc", configB : "xyz"}
      })
   ]

AuthLibModule.module.ts

export class AuthLibModule{ 
  static forRoot(configuration): ModuleWithProviders {
    console.log(configuration);
    return {
      ngModule: AuthLibModule,
      providers: [AuthLibService,{provide: 'config', useValue: configuration}]
    };
  }
}

AuthLibService.service :

export class AuthLibService{

  constructor(@Inject('config') private config:any) {
    console.log(config)
   }
}
Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
  • 1
    When I did that in a separate project, it worked; but moving it into my project doesn't so there is a problem with my project setup that I will need to figure out. Thank you for your answer. – Mohamed Talaat Harb Dec 26 '19 at 13:40
  • Hey @Adrita - when I do this Argument Type (configuration: {configB: string, configA: string}} is not assignable to parameter type Routes. I've tried this many times and can't get around this error - something seems wrong with my syntax - any thoughts? I can post more code if that helps. Thank you – Voltan Nov 02 '21 at 16:24
  • @Voltan Can you share your code in stackbiltz – Adrita Sharma Nov 03 '21 at 06:39
  • 1
    @Adrita - thank you for response, Intellij was giving me a bogus error. All working now, so I'll close this. – Voltan Nov 03 '21 at 16:31
  • 1
    @Voltan Great. If this helped you please upvote :) – Adrita Sharma Nov 04 '21 at 06:38