DEV Community

Discussion on: Secure authentication in Nuxt SPA with Laravel as back-end

Collapse
 
soonseek profile image
soonseek

HEEEEY, I love this article so much. I finally could have a little of sense what AUTH is.
After a little of time spending to overcome page reload issue, I customized bit of your code.
Key issue was that I have to make my client read cookie from SSR part.

1) To get cookie from SSR, install 'cookieparser'
npm i cookieparser -S

2) @ store/index.js
const cookieparser = process.server ? require('cookieparser') : undefined
and action part,
async nuxtServerInit ({ commit, dispatch }, {req}) {
const token = cookieparser.parse(req.headers.cookie)['x-access-token']
await commit('SET_TOKEN', token);
},

3) @ middleware/refreshToken.js
if (! store.state.token) {
store.dispatch('refreshToken')
.catch(errors => {
store.dispatch('logout');
});
}else{
}

Hope this helps some people who are struggling to overcome page reload issue.