1

I have a hard time to use an openId auth package with this config....

I have 3 modules :

module 1 : mainApplication module 2 : core module 3 : portal

I have a service (AuthService) in the core module, extending OAuthService from angular-oauth2-oidc.

It's just a way to isolate the dependency from angular-oauth2-oidc.

The mainApplication module is loading the portal & core service.

@NgModule({
  declarations: [
  ],
  entryComponents: [ 
  ],
  imports: [
    CommonModule,
    IsocialCoreModule.forRoot(),
    IsocialPortalModule.forRoot(portalConfig),
  ])}
  export class MyMainApp {}

My portalModule:

...
  imports: [
    MyCoreModule,
  ],
})
export class MyPortalModule {
  static forRoot(config: PortalConfig): ModuleWithProviders {
    return {
      ngModule: PortalModule,
      providers: [ConfigService, 
                 {provide: PortalConfigToken, useValue: config}, 
                 AuthService] // --> Error if not added
    };
  }
}

My coreModule:

@NgModule({
  imports: [
    HttpModule,
    OAuthModule.forRoot()
  ],
  declarations: [],
})
export class MyCoreModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: MyCoreModule,
      providers: [BasicGuardService, AuthService // Error if not added]
    };
  }
}

My config service :

  constructor(@Inject(PortalConfigToken) private portalConfig: PortalConfig, private authService: AuthService) {
    this.config = this.portalConfig;
    this.authService.configure(this.portalConfig.authConfig);
  }

It seems that the AuthService is instanciate more than one time...

And I lost the config beetween the moment where I configure the service and the moment I use it.

JulienD
  • 7,102
  • 9
  • 50
  • 84
bokzor
  • 413
  • 7
  • 19

1 Answers1

0

add the service (in providers) ONLY in the "Main Application". So, the imported modules in "Main Application" share the same service

Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • 1
    I tried it. But by doing that I have an exception saying that the provider is not defined. – bokzor Sep 25 '17 at 22:43
  • 1
    ERROR Error: No provider for AuthService! I have to add it as dependency in Core, Portal & MainApp – bokzor Sep 26 '17 at 09:21
  • Sorry, I'am a little newbie in Angular4. I thinked that if you use mainApplication, @NgModule({declarations: [..],imports[..],providers[AuthService]..)} you only have to import the AuthService in the Component you need – Eliseo Sep 26 '17 at 15:06
  • Yeah. I thought it too. – bokzor Sep 26 '17 at 15:28