I'm in need of adding a provider to facilitate sharing a variable with app.component.ts. My goal is to access the logged-in user data in real time within my app, but I'm struggling to correctly implement the provider in the constructor, resulting in the following error message:
Error: No Firebase App '[DEFAULT]' has been created - please call Firebase App.initializeApp()
Below is an overview of my code setup:
app.component.ts :
import { UsersServiceProvider } from '../providers/users-service/users-service';
@Component({
templateUrl: 'app.html',
})
export class MyApp {
@ViewChild(Nav) navCtrl: Nav;
rootPage:any = InicioPage;
constructor(public userServices : UsersServiceProvider) // along with other imports
platform.ready().then(() => {
statusBar.styleDefault();
splashScreen.hide();
});
var config = {
//configurations for the app and firebase
};
firebase.initializeApp(config);
var that = this;
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// Need to assign the variable on userServices HERE
}
})
};
user-service.ts
import { Injectable } from '@angular/core';
import {AlertController , Platform} from 'ionic-angular'
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import * as firebase from 'firebase';
@Injectable()
export class UsersServiceProvider {
public currentUser : any;
constructor(private platform: Platform, public alertCtrl: AlertController, public http: Http) {
}
setCurrentUser(user){
this.currentUser = user; // should be set here
}
}
All configurations are also properly set up in app.module.ts