Nuxt/examples/auth-jwt/store/index.js
2018-08-17 08:29:58 +02:00

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