I have a general question about accessing with user data with angularFire2. This might just be a question about using an observable or a promise.
Firebase documentation recommends accessing user data like so:
console.log( firebase.auth().currentUser ) //retrieved user data
The above works great, but In the angularFire2 documentation for v4.0 it recommends using afAuth.authState
this.afAuth.authState.subscribe(auth => console.log( auth ) ) //retrieved data in observable
Any recommendations or explanations would be great. I realize the observable binds to new data, but if I want to access the observable in multiple places is it okay to have multiple subscribes through out my app?, or is it better to just re-query the data with the firebase.auth() when it's needed. Also the subscribes with nested subscribes starts to get very complex, would it be better to just use firebase.auth().currentUser and prevent nested subscribes?
this.afAuth.authState.subscribe(auth => {
if(auth)
this.uid = auth.uid;
this.userProfileRef = db.object('/users/'+auth.uid,{ preserveSnapshot: true });
this.userProfileRef.subscribe(snapshot => {
console.log(snapshot.val() ); //ugh complexity
});
}
});
or
this.uid = firebase.auth().currentUser.uid;
this.userProfileRef = db.object('/users/'+auth.uid,{ preserveSnapshot: true });
this.userProfileRef.subscribe(snapshot => {
console.log(snapshot.val() );
});