-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Initialize the config dynamically outside app.module.ts #1095
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Suppose you could create your own token instead of using the default and set it up in an APP_INITIALIZER (this would require a valid user at all times): app.module.ts @Injectable()
export class SecondApp {
database: AngularFireDatabase;
auth: AngularFireAuth;
constructor() {}
initialize(app: firebase.app.App) {
this.database = new AngularFireDatabase(app);
this.auth = new AngularFireAuth(app);
}
}
export function firebaseLoader(secondApp: SecondApp) {
return () => {
const firstApp = firebase.initializeApp({
// Put firebase config for first app here
}, 'controlApp');
const auth = new AngularFireAuth(firstApp);
const db = new AngularFireDatabase(firstApp);
return auth.authState.filter(user => !!user).map(user => {
return db.object(`configs/${user.uid}`);
}).map(configObject => {
const secondAppConfig = firebase.initializeApp(configObject, 'secondApp');
secondApp.initialize(secondAppConfig);
}).first();
};
}
@NgModule({
...,
providers: [
SecondApp,
{
provide: APP_INITIALIZER,
useFactory: firebaseLoader,
deps: [ SecondApp ],
multi: true
}
]
})
export class AppModule { } Then instead of injecting AngularFireDatabase or AngularFireAuth you inject SecondApp and use the database and auth properties. You can use this approach without the initializer as well of course, e.g. using a route guard with a login form and just call SecondApp.initialize() as often as you like. Don't think you are bound to use the AngularFireDatabase or FirebaseApp tokens, it's more of a convenience. To make it cleaner you could perhaps name your apps as well when constructing them and delete the ones not required anymore (you can do app.delete() to free ressources of an app you don't need anymore). Then use a map on your service that and create the AngularFire Services for each of the apps. |
Closing since not an Issue, feel free to continue discussing. |
@kamshak Th errors are in: I am pretty new to firebase, just evaluating it for a new project so i think i make a stupid beginners error :) |
@ralph-fuechtenkort this solution works for me getting the Firebase configuration from a JSON assets file: fetch('/assets/config.json').then(resp => resp.json()).then(config => {
window['firebase_config'] = config.firebase;
window['config'] = config;
platformBrowserDynamic().bootstrapModule(AppModule).catch(err => console.error(err));
});
export function initializeApp(appConfig: AppConfigService) {
return appConfig.fireConfig()
}
@NgModule({
imports: [
AngularFireModule,
AngularFireAuthModule,
AngularFireStorageModule
],
providers: [
AppConfigService,
{
provide: FirebaseOptionsToken,
deps: [AppConfigService],
useFactory: initializeApp
}
],
bootstrap: [AppComponent]
})
export class AppModule {}
@Injectable({
providedIn: 'root'
})
export class AppConfigService {
static settings: IAppConfig;
constructor() {}
fireConfig() {
AppConfigService.settings = window['config'];
return window['firebase_config']
}
}
{
"production": true,
"name": "Hello world",
"firebase": {
"apiKey": "...",
"authDomain": "...",
"databaseURL": "...",
"projectId": "...",
"storageBucket": "...",
"messagingSenderId": "..."
}
}
export interface IAppConfig {
production: boolean;
name: string;
firebase: {
apiKey: string;
authDomain: string;
databaseURL: string;
projectId: string;
storageBucket: string;
messagingSenderId: string;
};
} I think it's similar on a ionic3 app. |
I'm trying to solve a use case where I need to connect to one database first to get some kind of a token and references of another database and then connect to the second database. I don't know in advance the config of the second database (only the first one). I can't hard code the second one in my app.
So, I need to be able to initialize a config to a firebase database dynamically. I already check #306 and #761 but none is giving me a real answer on how to perform it. And I checked already https://firebase.google.com/docs/configure/ but I would prefer to keep AngularFire2.
I'm using Ionic 3.5 and angularfire2 "4.0.0-rc.1".
I basically need to call AngularFireModule.initializeApp(secondDatabaseConfig) somehow later in my code not in my app.module.ts
Is it possible ? How would you solve it with AngularFire2 ?
I'm even ready to use a usual WebService to get the config to my second database, so I wouldn't need the first connection. But I'm blocked on instantiating the connection to Firebase dynamically.
The idea behind all of it is to provide to each of my customers their own firebase database (for security reasons) but where they all share the same app. The Firebase Java backend has total control of the database and I need to provide this Java backend to all of my customers...
Thank you for your amazing job
The text was updated successfully, but these errors were encountered: