location.hostname returns null or [Object%20object] in angular when building production mode

Viewed 444

I have a problem with my angular app. when I try to build with --prod parameter (this means aot is turn to true) location.hostname I have in my code returns null and I cannot interact with my API.

Now I have read it's because I have to use a factory so my code will be executed at runtime only, but I am not sure how to do.

Below the code I have used, which doesn't work becuase my url looks like this:

http://ipaddress/path/[Object%20object]/path/path/path/path/api/login/

[...]

const REDIRECT_URI = new InjectionToken<string>('REDIRECT_URI');

export function redirectUriFactory() {
  //return `${location.host}`;
  return window.location.host;
}


[...]


@NgModule({
  imports: [
    CommonModule,
  ],
  exports: [
    NbAuthModule,
  ],
  declarations: [],
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }

  static forRoot(): ModuleWithProviders {
    return <ModuleWithProviders>{
      ngModule: CoreModule,
      providers: [
        ...NB_CORE_PROVIDERS,
       {provide: REDIRECT_URI, useFactory: redirectUriFactory}
      ],
    };
  }
}

[...]



const NB_CORE_PROVIDERS = [
  ...DataModule.forRoot().providers,
  ...NbAuthModule.forRoot({
    providers: {
      email: {
        service: NbEmailPassAuthProvider,
        config: {
          delay: 0,
          token: { key: 'token' },
          logout: {
            endpoint: ''
          },
          login: {
            rememberMe: true,
            alwaysFail: false,
            endpoint: 'http://' + REDIRECT_URI +'/path/path/path/path/api/login/',
            method: 'post',
            redirect: {
              success: '/',
              failure: null,
            },
          },
        },
      },
    },
    [...]

any help will be very appreciated

0 Answers
Related