Vue, Firebase, Vuexfire: How to keep track of the current user?

253 Views Asked by At

I am building an app with vue and vuex and connect it to Firebase like this:

import firebase from 'firebase';
import "firebase/firestore";

firebase.initializeApp(firebaseConfig);
export const auth = firebase.auth();
export const db = firebase.firestore();

I then import auth and use createUserWithEmailAndPassword, signInWithEmailAndPassword and signOut to sign up, sign in and sign out users.

What I want to do now is keeping track of who is logged in and display their e-mail address (for example). I learnt that I can use onAuthStateChanged like

auth.onAuthStateChanged(function(user) {
   console.log(user);
});

But where do I put this? And how can I make the user variable available in the whole app?

I though of saving it to vuex, but I have never seen this and began to doubt that this is a good way. What is more, my mutations seem to be blocked by mutations: vuexfireMutations so that I could not save the user to the store even if I wanted to.

Thanks for any hints!


Edit: Found out that adding own mutations to vuexfireMutations works like this:

  setUser(state, payload) {
    state.user = payload;
  },
  setError(state, payload) {
    state.error = payload;
  },
  ...vuexfireMutations,
0

There are 0 best solutions below