Search code examples
vue.jsauthenticationcookiesnuxt.jsnuxtserverinit

nuxtServerInit does not receive auth cookies


I have nuxtServerInit function and it should receive all users' browser data including cookies, but it does not receive the auth module (but not every time!) cookies (I'm talking about JWT Bearer token).

async nuxtServerInit(store, { req }) {
    console.log(req.headers.cookie);
}

In the right case, it will show something like this: i18n_redirected=en; auth.strategy=local; auth._token.local=Bearer%20eyJhbG*******

But in other browsers (edge for example), ONLY after browser restart, it will output this: i18n_redirected=en. But then, if you update the site or open it in another tab it will be fine.

I can't understand WHY. It works so randomly... For example, in my main browser (chrome) it doesn't send the cookies every first time I launch (just as I said before), but then, when I just update the page it sends everything just fine. I tried to repeat this bug on purpose and it worked a few times and then stopped... But it works constantly in the Edge browser. Cookies are still there as they were before, but nuxt just don't receive them. I don't have some special cookies add-ons, everything works by its default options...

Is this some sort of a cache going on or what? Can someone help me with this, please.


Solution

  • Turns out Nuxt Auth module sets its cookies expires to empty string so it is just session based cookies after all, which are not sent to the server on axios request. I had to manually set the expiration date to auth module cookie settings in nuxt.config.js:

    auth: {
      ...
      cookie: {
        options: {
          expires: new Date(new Date().getTime()+20000000000).getTime(), //thats today + a year
          maxAge: 31622400
        }
      }
    },
    

    I still don't think this is right that it doesn't set the expiration date by itself. The cool thing about everything in nuxt, is that many things work just out of the box and the auth module shouldn't work like that with nuxt's native function that is not like rarely used. Had to waste 3 hours on this :(