The methods and properties in these files are the same as those we had in past exercises, except for the nuxtServerInit action:
// store/index.js
const cookie = process.server ? require('cookie') : undefined
export const actions = {
nuxtServerInit ({ commit }, { req }) {
if (
req
&& req.headers
&& req.headers.cookie
&& req.headers.cookie.indexOf('auth') > -1
) {
let auth = cookie.parse(req.headers.cookie)['auth']
commit('setAuth', JSON.parse(auth))
}
}
}
There is no server involved in the Nuxt SPA since nuxtServerInit is called by Nuxt from the server-side only. So, we will need a solution for that. We can use the Node.js js-cookie module to store the authenticated data on the client-side when the user logs in, which makes it the best candidate to replace the server-side cookie. Let's learn how to achieve this:
- Install the...