Nuxt/examples/auth-jwt/store/index.js
pooya parsa e7cc2757c3 refactor: update eslint-config to 1.x
Co-authored-by: Alexander Lichter <manniL@gmx.net>
2019-07-10 15:15:49 +04:30

27 lines
547 B
JavaScript

const cookieparser = process.server ? require('cookieparser') : undefined
export const state = () => {
return {
auth: null
}
}
export const mutations = {
setAuth (state, auth) {
state.auth = auth
}
}
export const 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)
}
}