'Best way to get Firebase user on each page load / component (Vue) [duplicate]

I need to get the current Firebase authenticated user on each component load in Vue. At the moment I have this in each beforeMount():

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
  } else {
    // User is signed out
    // ...
  }
})

I tried simplifying this to:

var user = firebase.auth().currentUser

It didn't work. Why doesn't it work, when the auth state hasn't changed? I'm confused by this. Is the first method the best way to accomplish this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source