[Vuejs]-Vue.js Vuex Firebase – Cannot get user from store

0👍

Make sure that the User is Auth, you can use the method onAuthStateChanged() of firebase auth in you main.js if you are using vue-cli. See the example:

in you main.js after firebase inicialization

// Initialize Firebase
    let config = {
       apiKey: "you_app_generated_key",
       authDomain: "youapp-randomkey.firebaseapp.com",
       databaseURL: "https://youapp-randomkey.firebaseio.com",
       projectId: "youapp-randomkey",
       storageBucket: "gs://youapp-randomkey.appspot.com/",
    };
    firebase.initializeApp(config);

    //If the event onAuthStateChanged() is emited
    //You can can verify if the user is auth or not.
    //If he isn't you can set null for the user.

    firebase.auth().onAuthStateChanged((user) => {

        if (user) {

            this.$store.dispatch('setUser', user) //set the User

        } else {

            this.$store.dispatch('setUser', null) //set null because he is notauth
        }

    })


    }
})

Now in any part of you project you can verify if there’s a User or not in you state.

If there’s a User, To display it, it depends of what user prop you need to display.

When need to display or get user: name, email and photo. I do:

let user = state.user
let user_id: user.uid
let user_name: user.displayName,
let user_photo: user.photoURL

.

Sorry for my english.

Leave a comment