1
0
mirror of https://github.com/nuxt/nuxt.git synced 2024-12-19 10:47:15 +00:00
Nuxt/examples/auth-jwt/store/index.js

33 lines
684 B
JavaScript
Raw Normal View History

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