mirror of
https://github.com/nuxt/nuxt.git
synced 2024-12-18 02:07:14 +00:00
33 lines
684 B
JavaScript
33 lines
684 B
JavaScript
import Vuex from 'vuex'
|
|
|
|
const cookieparser = process.server ? require('cookieparser') : undefined
|
|
|
|
const createStore = () => {
|
|
return new Vuex.Store({
|
|
state: {
|
|
auth: null
|
|
},
|
|
mutations: {
|
|
setAuth(state, auth) {
|
|
state.auth = auth
|
|
}
|
|
},
|
|
actions: {
|
|
nuxtServerInit({ commit }, { req }) {
|
|
let auth = null
|
|
if (req.headers.cookie) {
|
|
const parsed = cookieparser.parse(req.headers.cookie)
|
|
try {
|
|
auth = JSON.parse(parsed.auth)
|
|
} catch (err) {
|
|
// No valid cookie found
|
|
}
|
|
}
|
|
commit('setAuth', auth)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
export default createStore
|