nuxtServerInit不接收身份验证cookie [英] nuxtServerInit does not receive auth cookies

查看:82
本文介绍了nuxtServerInit不接收身份验证cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有nuxtServerInit函数,它应该接收所有用户的浏览器数据,包括cookie,但是它不接收auth模块(但不是每次都这样)cookie(我说的是JWT Bearer令牌).

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);
}

在正确的情况下,它将显示以下内容: i18n_redirected = en;auth.strategy = local;auth._token.local = Bearer%20eyJhbG *******

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

但是在其他浏览器(例如Edge)中,只有在浏览器重新启动后,它才会输出以下内容: i18n_redirected = en .但是,如果您更新该站点或在另一个选项卡中将其打开,就可以了.

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.

我不明白为什么.它的工作原理如此随机……例如,在我的主浏览器(chrome)中,它不会在我第一次启动时发送cookie(就像我之前所说的),但是,当我更新页面时,它只会发送所有信息美好的.我试图故意重复此错误,但该错误几次起作用,然后停止了……但是它在Edge浏览器中始终有效.Cookies仍然像以前一样在那里,但是nuxt只是不接收它们.我没有一些特殊的Cookie加载项,所有功能都可以通过其默认选项进行操作...

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.

推荐答案

将Nuxt Auth模块的cookie设置为 expires 为空字符串,因此毕竟它只是基于会话的cookie,不会发送到axios请求上的服务器.我必须在 nuxt.config.js 中的auth模块cookie设置中手动设置到期日期:

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
    }
  }
},

我仍然认为这不是正确的,因为它没有自行设置到期日期.关于nuxt中所有功能的最酷的事情是,许多功能开箱即用,而auth模块不应该像nuxt的本机函数那样工作,这种本机函数很少使用.不得不在此浪费3个小时:(

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 :(

这篇关于nuxtServerInit不接收身份验证cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆