简体   繁体   中英

Vue.js & Firebase 'User' info

I'm developing an app using Vue.js and Firebase.

I've managed to get the login working using email/password, and can successfully store the firebase.auth() user info in the Vue.js state under state.currentUser .

However, as per the Firebase guidelines, I have more information stored under a '/users' ref. Eg. /users/uid

I'm wanting to pull this data into the vuex store too (state.userMeta), so I can use it around the site, however it never works after a refresh. If I don't refresh, logout and then log back in, it populates the state fine.

Any help would be massively appreciated!

James

auth.js

import { auth, db } from '@/config/database'
import store from '@/vuex/store'

const init = function init () {
  auth.onAuthStateChanged((user) => {
    if (user) {
      store.dispatch('setUser', user)
    } else {
      // User is signed out.
      store.dispatch('setUser', null)
    }
  }, (error) => {
    console.log(error)
  })
}

export { init }

store.js

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { db } from '@/config/database'

Vue.use(Vuex)

const state = {
  currentUser: null,
  userMeta: null
}

const mutations = {
  SET_USER (state, user) {
    if (user) {
      state.currentUser = user
      db.ref('users/' + user.uid).on('value', function (s) {
        state.userMeta = s.val()
      })
    } else {
      state.currentUser = null
      state.userMeta = null
    }
  }
}

const actions = {
  setUser ({commit}, user) {
    commit('SET_USER', user)
  }
}

const getters = {
  currentUser: state => state.currentUser
}

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters,
  plugins: [createPersistedState()]
})

App.vue

<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
import * as auth from '@/config/auth'

const initApp = function initApp () {
  auth.init()
}

export default {
  name: 'app',
  created: initApp
}
</script>

<style lang="scss" src="./assets/sass/style.scss"></style>

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueFire from 'vuefire'
import router from './router'

Vue.config.productionTip = false
Vue.use(VueFire)

import store from '@/vuex/store'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})

Vuex state is cleared upon page refresh.

I sorted this out using this method

In main.js

new Vue({
el: '#app',
router,
store,
created() {
    Firebase.auth().onAuthStateChanged((user) => {
            if (user) {
                this.$store.state.user = this.$firebase.auth().currentUser
            } else {
                this.$store.state.user = null
            }
        })
    },
render: h => h(App)
})

So when there is a refresh, your state will be sync automatically.

In your components

computed: {
    user() {
        return this.$store.state.user
    }
},

After a refresh, trying to use this.$firebase.auth().currentUser doesn't not work - but setting the state again via Auth changed seems to do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM