How to get the current user key at login

How to get the current user key that has registered. I've tried this way.

openMapPage()
  {
   var ref = firebase.database().ref("request");
    ref.once("value").then((snapshot) => { // <------ Here!
        var a = snapshot.exists();  // true
        var c = snapshot.hasChild("reqdetails"); // true
        var d = snapshot.child('reqdetails').exists();
        var requestsKey = snapshot.key;
        var requestsValue = snapshot.val();

          this.afAuth.authState.take(1).subscribe(data =>{
      this.profileData = this.af.object(data.uid);
                console.log(this.profileData);
    })
        snapshot.forEach((childSnapshot) => { // <------ And here!
            var requestKey = childSnapshot.key;
            var requestValue = childSnapshot.val();

            var reqdetails = requestValue.reqdetails;
            if (reqdetails) {
                this.data = requestKey;
                console.log(this.data);
                //this.arr.push(requestKey);
                //console.log(this.arr);

                 this.getRequest = this.angFire.list('request', {
                   query: {
                   orderByChild: 'reqdetails',
                   startAt: 'reqdetails'
            }
         }) 


            }
        });

    });     
  }

      

I tried to get the current user in this piece of code:

 this.afAuth.authState.take(1).subscribe(data =>{
      this.profileData = this.af.object(data.uid);
                console.log(this.profileData);
    })

      

When I console log for profileData I get this thing:

enter image description here

+3


source to share


1 answer


With, Javascript

you can get a callback onAuthStateChanged()

when a user is logged in or logged out using firebase.

To get the currentUser key, you can use:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    var currentUserKey = firebaseAuth.currentUser.uid
  } else {
    // No user is signed in.
  }
});

      



Hope this helps.

See Firebase Auth for details

0


source







All Articles