设置的cookie没有`SameSite`属性。 …但是我做到了

问题描述 投票:0回答:1
A cookie associated with a cross-site resource at http://tetris-back-end.herokuapp.com/ was set without the `SameSite` attribute.

我尝试在前端设置cookie时收到该错误。这是在后端设置我的cookie的方法:

const sessionOptions: Options = {
  store: new RedisStore({
    client: redis as any,
  }),
  name: "qid",
  secret: String(process.env.SECRET),
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: process.env.NODE_ENV === "development",
    secure: process.env.NODE_ENV === "production",
    sameSite: "none",
    maxAge: 1000 * 60 * 60 * 24 * 7 * 365, // 7 years
  },
};

因此,同一站点被设置为none,但是我仍然遇到该错误。

我进行了一些探索,并在我的开始消息中控制了节点env。

app.listen(process.env.PORT, () => {
  console.log(message, `NODE ENV: ${process.env.NODE_ENV} 🛠`);
  });

[在本地打印development,在Heroku日志中打印production

当我在本地设置cookie时,它可以工作,但是出现错误:

A cookie associated with a resource at http://localhost/ was set with `SameSite=None` but without `Secure`. A future release of Chrome will only deliver cookies marked `SameSite=None` if they are also marked `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5633521622188032.

所以本地是相同的代码(我在master上,并且都同步了)。当我这样做时它可以工作,但是由于NODE_ENV ===“ development”,因此将安全性设置为false。

我想这里很重要的一点是,它确实看到我已在本地设置SameSite = None,但它并没有在生产时就采用。

为什么同一站点属性在本地会被识别为已设置,但不能在生产版本上识别??

任何帮助都将是巨大的!谢谢!

Check out back end repo.

Check out front end repo.

cookies https session-cookies backend production
1个回答
0
投票

我在会话选项中将cookie对象注释掉,并且效果很好!

我引用了这篇对我有帮助的帖子:Cookie not set with express-session in production

const sessionOptions: Options = {
  store: new RedisStore({
    client: redis as any,
  }),
  name: "qid",
  secret: String(process.env.SECRET),
  resave: false,
  saveUninitialized: false,
  // cookie: {
  //   httpOnly: process.env.NODE_ENV === "development",
  //   // secure: process.env.NODE_ENV === "production",
  //   secure: "auto",
  //   sameSite: "none",
  //   maxAge: 1000 * 60 * 60 * 24 * 7 * 365, // 7 years
  // },
};
© www.soinside.com 2019 - 2024. All rights reserved.