Advertisement
sayhicoelho

Vuex

Jul 19th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // src/vuex/store.js
  2.  
  3. import Vue from 'vue'
  4. import Vuex from 'vuex'
  5. import router from '../router'
  6. import firebase from '../config/firebase'
  7.  
  8. Vue.use(Vuex)
  9.  
  10. export default new Vuex.Store({
  11.   state: {
  12.     firstLoad: true,
  13.     routing: false
  14.   },
  15.  
  16.   mutations: {
  17.     SET_FIRST_LOAD (state) {
  18.       state.firstLoad = false
  19.     },
  20.     SET_ROUTING (state, payload) {
  21.       state.routing = payload
  22.     }
  23.   },
  24.  
  25.   actions: {
  26.     FIRST_LOAD ({ commit }) {
  27.       commit('SET_FIRST_LOAD')
  28.     },
  29.     ROUTE_PENDING ({ commit }) {
  30.       commit('SET_ROUTING', true)
  31.     },
  32.     ROUTE_COMPLETE ({ commit }) {
  33.       commit('SET_ROUTING', false)
  34.     },
  35.     FETCH_AUTH ({ state, dispatch }) {
  36.       return new Promise(resolve => {
  37.         if (state.firstLoad) {
  38.           firebase.auth().onAuthStateChanged(user => {
  39.             if (state.routing) {
  40.               resolve(user)
  41.             } else if (!state.routing && !user) {
  42.               router.replace({
  43.                 name: 'Login',
  44.                 query: {
  45.                   redirect: window.location.hash.substr(1)
  46.                 }
  47.               })
  48.             }
  49.           })
  50.  
  51.           dispatch('FIRST_LOAD')
  52.         } else {
  53.           resolve(firebase.auth().currentUser)
  54.         }
  55.       })
  56.     }
  57.   }
  58. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement