Authentication Nuxt auth SSR cookie仅在关闭浏览器并加载页面时出现问题

Authentication Nuxt auth SSR cookie仅在关闭浏览器并加载页面时出现问题,authentication,cookies,nuxt.js,server-side-rendering,Authentication,Cookies,Nuxt.js,Server Side Rendering,我使用nuxt和nuxt auth next,并使用LaravelAPI后端 当我完全关闭浏览器并加载页面时,nuxt无法读取服务器端的cookies,也无法理解我已登录,在客户端上,它知道我有cookie,因此我的服务器端和客户端会根据用户登录情况加载一些组件,然后,显然会发生这种情况: [Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is l

我使用nuxt和nuxt auth next,并使用LaravelAPI后端
当我完全关闭浏览器并加载页面时,nuxt无法读取服务器端的cookies,也无法理解我已登录,在
客户端
上,它知道我有cookie,因此我的
服务器端
客户端
会根据用户登录情况加载一些组件,然后,显然会发生这种情况:

[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or missing <tbody>. Bailing hydration and performing full client-side render.
控制台上的结果如下:

Nuxt SSR
cookie = undefined

Client: 
cookie = Bearer blablabla

我不久前就有这个问题。 当您的项目在服务器端运行时,您可以使用此包“cookieparser”访问您的cookie

这是我在服务器端获取用户令牌的方式:

const cookieparser = process.server ? require('cookieparser') : undefined  


async nuxtServerInit ({ commit }, { req }) {

   if (req.headers.cookie) {
      const parsed = cookieparser.parse(req.headers.cookie)

     // if we have a token , get user data with api
     if (parsed.access_token) {
       
     }
  }
}

我不久前就有这个问题。 当您的项目在服务器端运行时,您可以使用此包“cookieparser”访问您的cookie

这是我在服务器端获取用户令牌的方式:

const cookieparser = process.server ? require('cookieparser') : undefined  


async nuxtServerInit ({ commit }, { req }) {

   if (req.headers.cookie) {
      const parsed = cookieparser.parse(req.headers.cookie)

     // if we have a token , get user data with api
     if (parsed.access_token) {
       
     }
  }
}
发件人:

当前会话结束时,将删除会话cookie。浏览器 定义“当前会话”何时结束,某些浏览器使用会话 重新启动时还原,这可能会导致会话cookie持续 无限长。在指定日期删除永久cookie 通过Expires属性,或在 最大年龄属性

如果您使用nuxt auth,并且希望避免会话cookie过期,从而解决关闭浏览器问题,则可以在nuxt.config.js中设置expires和maxAge属性:

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

当前会话结束时,将删除会话cookie。浏览器 定义“当前会话”何时结束,某些浏览器使用会话 重新启动时还原,这可能会导致会话cookie持续 无限长。在指定日期删除永久cookie 通过Expires属性,或在 最大年龄属性

如果您使用nuxt auth,并且希望避免会话cookie过期,从而解决关闭浏览器问题,则可以在nuxt.config.js中设置expires和maxAge属性:

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

此代码来自

问题在于
req.headers.cookie
在我的nuxt应用程序中是
未定义的
。重要的是,只有当我完全关闭浏览器并重新打开我的网站时,这才是未定义的。如果我再次刷新页面,
req.headers.cookie
将再次可用!问题是在我的nuxt应用程序中,
req.headers.cookie
未定义。重要的是,只有当我完全关闭浏览器并重新打开我的网站时,这才是未定义的。如果我再次刷新页面,
req.headers.cookie
将再次可用!同样的问题,你找到解决办法了吗?你找到解决办法了吗?请告诉我们最新情况。同样的问题,你找到解决方案了吗?你找到解决方案了吗?请告诉我们最新情况。