mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 16:12:12 +00:00
27 lines
545 B
JavaScript
27 lines
545 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)
|
|
}
|
|
}
|