0

I'm attempting to access some value providers inside a factory function inside the AppModule (OidcConfigService below). Normally, inside components, I can just have the values I'm trying to get via constructor injection using @Inject('PROVIDER_NAME'). In this case, it's just a factory function so there's no constructor for me to get my values. You can see my attempt at getting the values inside the OidcConfigService provider, but these are just failed attempts.

AppModule (the function configureAuth at the bottom is where I'm trying to have the values sent)

import { HttpClientModule } from '@angular/common/http';
import { APP_INITIALIZER, NgModule, Input, Inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
//Angular modules
import { BrowserModule } from '@angular/platform-browser';
//Third-party modules
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { AuthModule, OidcConfigService, LogLevel, PublicEventsService, EventTypes } from 'angular-auth-oidc-client';
import { MDBBootstrapModule } from 'angular-bootstrap-md';
import { ConvertDatetimeToLocalPipe } from 'src/app/pipes/datetime/convert-datetime-to-local-pipe';
import { AppRoutingModule } from './app-routing.module';
//Components
<<LIST OF COMPONENTS>>

@NgModule({
  declarations: [
     <<LIST OF COMPONENTS>>
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    FormsModule,
    NgbModule,
    AuthModule.forRoot(),
    CommonModule,
    MDBBootstrapModule.forRoot()
  ],
  providers: [
    RequestService,
    ConvertDatetimeToLocalPipe,
    OidcConfigService,
    {
        provide: [APP_INITIALIZER, { provide: AUTH_URL, useValue: AppConfig }],
        useFactory: configureAuth,
        deps: [OidcConfigService, @Inject('AUTH_URL')],
        multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(private readonly eventService: PublicEventsService) {
      this.eventService
          .registerForEvents()
          .pipe(filter((notification) => notification.type === EventTypes.ConfigLoaded))
          .subscribe((config) => {
              console.log('ConfigLoaded', config);
          });
  }
}

export function configureAuth(oidcConfigService: OidcConfigService) {
  return () =>
      oidcConfigService.withConfig({
        //stsServer: this._authUrl,
        stsServer: 'http://localhost:5999',
        redirectUrl: `${window.location.origin}/postloginloading`,
        clientId: 'admin_web_app_client',
        responseType: 'code',
        scope: 'openid profile role ReviewApi UserApi',
        postLogoutRedirectUri: window.location.origin,
        forbiddenRoute: '/forbidden',
        unauthorizedRoute: '/unauthorized',
        silentRenew: true,
        silentRenewUrl: `${window.location.origin}/silent-renew.html`,
        historyCleanupOff: true,
        autoUserinfo: true,
        logLevel: LogLevel.Warn,
        maxIdTokenIatOffsetAllowedInSeconds: 10,
        storage: localStorage //If not working, change back to sessionStorage
      });
}

main.ts (these are the values I'm trying to access)

  const providers = [
    { provide: 'BASE_URL', useFactory: getBaseUrl, deps: [] },
    { provide: 'AUTH_URL', useValue: 'http://localhost:5999' },
    { provide: 'REVIEWAPI_URL', useValue: 'http://localhost:5101' },
    { provide: 'USERAPI_URL', useValue: 'http://localhost:5105' }
  ];

  platformBrowserDynamic(providers).bootstrapModule(AppModule)
  .catch(err => console.error(err));

Thanks in advance!

  • 1
    Try injecting the `Injector` as a dependency (i.e. `deps: [OidcConfigService, Injector]`) and use `injector.get(AUTH_URL)` to resolve the value of `AUTH_URL` at runtime – Joel Kornbluh Jun 15 '20 at 21:54
  • 1
    This might answer your question: https://stackoverflow.com/questions/50742028/angular-di-injecting-a-value-token-into-factory-provider – Joel Kornbluh Jun 15 '20 at 22:00
  • Wow, your first comment is exactly what I needed! Feel free to post as an actual answer and I'll mark it as solved. – user3494474 Jun 15 '20 at 22:26

0 Answers0