-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
182 lines (174 loc) · 5.54 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import Vue from 'vue'
import Vuex from 'vuex'
import axios from './axios-auth' // for REST into Firebase auth
import globalAxios from 'axios' // for REST into Firebase RTDB
import router from './router'
import firebaseConfig from './gcp.js';
Vue.use(Vuex)
export default new Vuex.Store({
state: {
idToken: null,
userId: null,
user: null
},
mutations: {
authUser (state, userData) {
state.idToken = userData.token
state.userId = userData.userId
},
storeUser (state, user) {
state.user = user
},
clearAuthData (state) {
state.idToken = null
state.userId = null
}
},
actions: {
setLogoutTimer ({commit}, expirationTime) {
setTimeout(() => {
commit('clearAuthData')
router.replace('/signin')
}, expirationTime * 1000) // * 1000 for milliseconds
},
signup ({commit, dispatch}, authData) {
// build signUp string for GCP/Firebase REST API
const signUpString = '/accounts:signUp?key='
+ firebaseConfig.options_.apiKey;
axios.post(signUpString, {
email: authData.email,
password: authData.password,
returnSecureToken: true
})
.then(res => {
// console.log(res)
// commit to Vuex store
commit('authUser', {
token: res.data.idToken,
userId: res.data.localId
})
// store session details in local Storage for Auto Login if token is not expired
const now = new Date()
const expirationDate = new Date(now.getTime() + res.data.expiresIn * 1000)
localStorage.setItem('token', res.data.idToken)
localStorage.setItem('userId', res.data.localId)
localStorage.setItem('expirationDate', expirationDate)
// store User in Firebase RTDB
dispatch('storeUser', authData)
router.replace('/dashboard')
dispatch('setLogoutTimer', res.data.expiresIn)
})
.catch(error => console.log(error));
},
login ({commit, dispatch}, authData) {
// build signIn string for GCP/Firebase REST API
const signInString = '/accounts:signInWithPassword?key='
+ firebaseConfig.options_.apiKey;
axios.post(signInString, {
email: authData.email,
password: authData.password,
returnSecureToken: true
})
.then(res => {
// console.log(res)
// store session details in local Storage for Auto Login if token is not expired
const now = new Date()
const expirationDate = new Date(now.getTime() + res.data.expiresIn * 1000)
localStorage.setItem('token', res.data.idToken)
localStorage.setItem('userId', res.data.localId)
localStorage.setItem('expirationDate', expirationDate)
commit('authUser', {
token: res.data.idToken,
userId: res.data.localId
})
router.replace('/dashboard')
dispatch('setLogoutTimer', res.data.expiresIn)
})
.catch(error => console.log(error));
},
tryAutoLogin ({commit}) {
const token = localStorage.getItem('token')
if (!token) {
return
}
const expirationDate = localStorage.getItem('expirationDate')
const now = new Date()
if (now >= expirationDate) { // token expired
return
}
// if not returned so far we have a valid not expired token
const userId = localStorage.getItem('userId')
commit('authUser', {
token: token,
userId: userId
})
},
logout ({commit}) {
commit('clearAuthData')
localStorage.removeItem('expirationDate')
localStorage.removeItem('token')
localStorage.removeItem('userId')
router.replace('/signin')
},
// store user in Firebase RTDB
storeUser ({commit, state}, payload) {
if (!state.idToken) {
return
}
// Milan's Answer to storing/fetching user data - Safer with no Pass storing!
const userData = {
age: payload.age,
country: payload.country,
email: payload.email,
interests: payload.interests
}
// globalAxios.post('/users.json' + '?auth=' + state.idToken, userData) // Max course code
// Bellow Milan's Answer to storing/fetching user data
globalAxios.put(`users/${state.userId}.json?auth=${state.idToken}`, userData)
.then(res => console.log(res))
.catch(error => console.log(error))
},
// fetch user data from Firebase RTDB
// Milan's Answer to storing/fetching user data
fetchUser: ({ commit, state }) => {
if (!state.idToken) {
return
}
globalAxios
.get(`users/${state.userId}.json?auth=${state.idToken}`)
.then(({ data }) => {
commit("storeUser", data)
})
.catch(error => console.error(error))
}
// Max Vue Code 361 - 363
// fetchUser ({commit, state}) {
// if (!state.idToken) {
// return
// }
// globalAxios.get('/users.json' + '?auth=' + state.idToken)
// .then(res => {
// // console.log(res);
// const data = res.data
// const users = []
// for (let key in data) {
// const user = data[key]
// user.id = key
// users.push(user)
// }
// // console.log(users)
// // this.email = users[0].email
// commit('storeUser', users[0])
// })
// .catch(error => console.log(error))
// }
},
getters: {
user (state) {
return state.user
},
isAuthenticated (state) {
return state.idToken !== null
}
}
})