Skip to content Skip to sidebar Skip to footer

Is It Possible To Bring The "user" Object To The "top Level" And Access It From Outside The "on.authsatechanged(user => .... }"?

I am trying to define a ref in vuefire to firebase path that depends on the current User. Is it possible to bring the 'user' object to the 'top level' and access it from outside th

Solution 1:

Simply adding it to the window object will do.

window.userRef = userRef;

e.g.

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    let userRef = firebase.database().ref('users').child(user.uid);
    window.userRef = userRef;
    let buildingsRef = userRef.child('buildings');
    // userRef returns the user uid 
  }
});

console.log(userRef); // no longer undefined only if this is called after `onAuthStateChanged` response. Thanks @EmileBergeron 

Post a Comment for "Is It Possible To Bring The "user" Object To The "top Level" And Access It From Outside The "on.authsatechanged(user => .... }"?"