Since our authenticated data with JWT has been hashed and stored in a cookie by js-cookie as auth, we will need to read and parse this cookie whenever we need it. This is where the Node.js module, cookie, comes in. Again, we have used this Node.js module in past chapters but we haven't talked about it.
The cookie Node.js module is an HTTP cookie parser and serializer for HTTP servers. It is used to parse the cookie header on the server side. Let's take a look at how we can use it on the auth cookie in the following steps:
- Use the if ternary condition to import the cookie Node.js module when our Nuxt app is processed on the server side only:
// store/index.js
const cookie = process.server ? require('cookie') : undefined
- Use the parse function from the cookie Node.js module to parse the auth cookie in the HTTP request headers in the nuxtServerInit action as follows:
// store/index.js
export const actions ...