简体   繁体   English

部署到 heroku 时,texpress-session 未在反应应用程序中设置 cookie

[英]texpress-session not setting cookie in react app when deployed to heroku

I am unable to set session cookies in the browser with my MERN stack app.我无法使用我的 MERN 堆栈应用程序在浏览器中设置 session cookies。 When both the express server and the react front end are running locally the cookies are set no problem.当 express 服务器和 react 前端都在本地运行时,cookies 设置没有问题。 The problem raises itself after I deployed the express backend to heroku.在我将 express 后端部署到 heroku 后,问题就出现了。 I tried setting the "sameSite" cookie option to "lax" or to false even but the problem persists.我尝试将“sameSite”cookie 选项设置为“lax”或设置为false ,但问题仍然存在。

here's the configuration I use for express session.这是我用于快递 session 的配置。

sessionConfig = {
secret: process.env.SESSION_SECRET,
resave: false,

saveUninitialized: true,
cookie: {
  secure: false,
  httpOnly: true,
  expires: expiryDate,
  sameSite: "lax",
},
store: MongoStore.create({
  mongoUrl: process.env.MONGO_CONNECTION_STRING,
}),
}

app.set("trust proxy", 1);
app.use(cors({ origin: true, credentials: true }));
app.use(session(sessionConfig));

EDIT: to further clarify.编辑:进一步澄清。 if I visit the root route of my express app from the browser the cookies are set normally even when the app is deployed.(IE: if I type the url of the deployed heroku app, the session cookie is set) if I make the call from the react front end that is running locally though( using fetch api) to the deployed backend, the cookie is not set. if I visit the root route of my express app from the browser the cookies are set normally even when the app is deployed.(IE: if I type the url of the deployed heroku app, the session cookie is set) if I make the call从本地运行的反应前端(使用 fetch api)到部署的后端,cookie 没有设置。 This leads me to think it could have something to do with the same site cookie option.这使我认为它可能与相同的站点 cookie 选项有关。

I realized what was causing the problem.我意识到是什么导致了这个问题。 It was a security feature in chrome that doesn't allow setting sameSite cookie option to none, unless the cookie is also set to secure.这是 chrome 中的一项安全功能,不允许将 sameSite cookie 选项设置为 none,除非 cookie 也设置为安全。 You can read more about it here issue with cross-site cookies: how to set cookie from backend to frontend or here https://medium.com/@arcagarwal/same-site-changes-in-chrome-1c86973454f9您可以在此处阅读有关跨站点 cookies 的更多信息:如何将 cookie 从后端设置到前端或此处https://medium.com/@arcagarwal/same-site-changes-in-chrome-1c86973454f9

I had this same issue,我有同样的问题,

Have you looked into the domain & path option in the cookie config?您是否查看过 cookie 配置中的domainpath选项?

  • domain : a string indicating the domain of the cookie (no default). domain : 表示 cookie 域的字符串(无默认值)。
  • path : a string indicating the path of the cookie (/ by default). path : 表示 cookie 路径的字符串(默认为 /)。

Refer to my answer: https://stackoverflow.com/a/67341751/12408623参考我的回答: https://stackoverflow.com/a/67341751/12408623

Are you using CRA?你在用 CRA 吗? Is your MERN app present in a Monorepo?您的 MERN 应用程序是否存在于 Monorepo 中?

Fixes:修复:

1.Bundle the react app together with the server. 1.将 react 应用与服务器捆绑在一起。

const express = require('express');
const path = require('path');
const app = express();
const PORT = 9000;

// You'll need to path join to your react build directory
app.use(express.static(path.join(__dirname, 'build')));

app.get('/', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(PORT);

2.Use JWTs to maintain token-based sessions. 2.使用 JWT 维护基于令牌的会话。

I would say this approach would be more robust.我会说这种方法会更健壮。 However, JWTs are not ideal in every situation but should suffice in most.然而,JWT 并非在所有情况下都是理想的,但在大多数情况下就足够了。

If you have a monorepo MERN codebase where you decide to use JWTs.如果您有一个 monorepo MERN 代码库,您决定在其中使用 JWT。 Then you can deploy both your client and server in different domains/platforms.然后,您可以在不同的域/平台中部署您的客户端和服务器。

Check out this blog post I wrote about deploying a subdirectory to Heroku查看我写的关于将子目录部署到 Heroku 的这篇博文

https://shrynk.jagankaartik.live/tSepqC1qKF https://shrynk.jagankaartik.live/tSepqC1qKF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM