-
Notifications
You must be signed in to change notification settings - Fork 33
/
store.js
69 lines (66 loc) · 1.89 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Vue from 'vue';
import Vuex from 'vuex';
import {db} from './main';
import createPersistedState from 'vuex-persistedstate';
// Tell Vue to use the Vuex store
Vue.use(Vuex);
// Initialize and export store
export default new Vuex.Store({
plugins: [createPersistedState()],
name: 'store',
state: {
user: {
loggedIn: false,
data: null,
profile: null
}
},
getters: {
// This makes it able to access the current logged in user data
user(state) {
return state.user
}
},
mutations: {
SET_LOGGED_IN(state, value) {
state.user.loggedIn = value
},
SET_USER(state, data) {
state.user.data = data
},
SET_PROFILE(state, profile) {
state.user.profile = profile
}
},
actions: {
// Allows to fetch the current user's authentication data after they log in
fetchUser({commit}, user) {
commit('SET_LOGGED_IN', user !== null);
if (user) {
commit('SET_USER', {
displayName: user.displayName,
email: user.email,
localId: user.uid
});
const ref = db.collection('users').doc(user.uid);
ref.get().then(function (doc) {
commit('SET_PROFILE',
doc.data()
)
})
} else {
commit('SET_USER', null);
commit('SET_PROFILE', null)
}
},
// Sets the profile data of a user
fetchProfile({commit}, userUID) {
const ref = db.collection('users').doc(userUID);
ref.get().then(function (doc) {
commit('SET_PROFILE',
doc.data()
)
})
}
}
})