AngularFire2 getting UID and using in any page

Viewed 1227

In Ionic(AngularFire2) application, for every page i need to get Firebase User UID, to get related data from Firebase database. For example, on Firebase there is a node('/users') and its key is FireBase Auth User UID.

  {
  "users" : {
    "LYW6i8sTbWauCvkXULIbszIWOdm1" : {
      "cellPhone" : "123321",
      "name" : "Name1",
      "userType" : "0"
    },
    "i4KSAzO2UhakowLKOZCa0ZeohRH3" : {
      "cellPhone" : "43343",
      "name" : "Name2",
      "userType" : "1"
    }
  }
}

I am using observables to get the UID, for every page again and again. I think there will be lots of subscriber and everytime ionic application makes request to Firebase Auth Server to get user UID ?

For example.

export class AccountProfilePage {

      user = {} as User
  constructor(public navCtrl: NavController, public navParams: NavParams, 
    private afDb:AngularFireDatabase, private afAuth:AngularFireAuth) {
  }

  updateProfile(){
      this.afAuth.authState.subscribe(authState=>{
        this.afDb.object('users/'+authState.uid).update({
          cellPhone:this.user.cellPhone
        })
      })
  }
}

Is it the correct way ?

1 Answers
Related