2018-08-17 05:49:06 +00:00
|
|
|
import Vuex from 'vuex'
|
|
|
|
|
|
|
|
const cookieparser = process.server ? require('cookieparser') : undefined
|
|
|
|
|
|
|
|
const createStore = () => {
|
|
|
|
return new Vuex.Store({
|
|
|
|
state: {
|
|
|
|
auth: null
|
|
|
|
},
|
|
|
|
mutations: {
|
2018-08-17 06:29:58 +00:00
|
|
|
setAuth(state, auth) {
|
|
|
|
state.auth = auth
|
2018-08-17 05:49:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
nuxtServerInit({ commit }, { req }) {
|
2018-08-17 06:29:58 +00:00
|
|
|
let auth = null
|
2018-08-17 05:49:06 +00:00
|
|
|
if (req.headers.cookie) {
|
|
|
|
const parsed = cookieparser.parse(req.headers.cookie)
|
2018-08-17 06:29:58 +00:00
|
|
|
try {
|
|
|
|
auth = JSON.parse(parsed.auth)
|
|
|
|
} catch (err) {
|
|
|
|
// No valid cookie found
|
|
|
|
}
|
2018-08-17 05:49:06 +00:00
|
|
|
}
|
2018-08-17 06:29:58 +00:00
|
|
|
commit('setAuth', auth)
|
2018-08-17 05:49:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default createStore
|