2

As per my current requirement I need to create a library to authenticate user, I chose firebase for this purpose. User of this library is expected to provide environment variables that will initialise AngularFireModule from App.

SocialLoginLibModule (Library module installed)

import { AngularFireModule } from '@angular/fire';
//for testing environment variables were placed in the library
import { environment } from '../environments/environment';
import { AngularFireAuthModule } from '@angular/fire/auth';
import { AngularFirestoreModule } from '@angular/fire/firestore';

@NgModule({
   declarations: [],
   imports: [
     CommonModule,
     //Currently using local environment
     //Need to get it from App
     AngularFireModule.initializeApp(environment.firebase),
     AngularFireAuthModule,
     AngularFirestoreModule
   ],
   providers: [],
   exports: []
})

export class SocialLoginLibModule {
   public static forRoot(environment:any): ModuleWithProviders {
     return {
       ngModule: SocialLoginLibModule,
       providers: [
        {
          provide: 'env',
          useValue: environment
        }
       ]
    }
  }
};

I tried and successfully received the environment variables using the forRoot() method in the module, is there a possible way to use this. I cannot initialise AngularFireModule later in a service or a component as it gives Static-Injector Error. Can it be done via using OS Environment Variables or any other way.

PS: Started coding 6 months back please spare lack of technical jargon. Deliberately removed unnecessary imports.

Rohan Sharma
  • 405
  • 1
  • 3
  • 11
  • I ran into the same situation and found the solution in another post and it just work. https://stackoverflow.com/a/54831986/2396490 – YGLin Mar 06 '19 at 08:34

0 Answers0