Express 不发送跨域 cookie

Express not sending cross domain cookies

简介

我在发送跨域会话 cookie 时遇到问题,搜索后我更加困惑了。 最初我在 foo.vercel.com 上有一个客户端(Next app),在 bar.heroku.com 上有一个 api(Express)。 发送和保存 cookie 在本地主机上正常工作,但在部署后我遇到了问题,默认情况下 sameSite 设置为 lex。所以我把它改成了none。但是我发现 sameSite none 需要在 cookie 集上设置 secure 标志,所以我也更改了它。

问题

secure 设置为 true 后,将不再发送 cookie。

If secure is set, and you access your site over HTTP, the cookie will not be set.

我的服务器和客户端都托管在 HTTPS 上。我检查了 heroku 上的日志,请求协议是 HTTPS... 但是当我 console.log 来自 GraphQL 解析器的 request.protocol 协议是 HTTP 时:

看到后this

There's no such thing as cross domain cookies. You could share a cookie between foo.example.com and bar.example.com but never between example.com and example2.com

我将客户端移至 baz.heroku.com,但问题仍然存在。

为了检查一切是否仍然有效,我在 chrome://flags 中禁用了 没有 SameSite 的 Cookie 必须是安全的 并且它工作正常。

问题

  1. 是否可以跨域设置cookie?
  2. 为什么 POST 方法与我在 express 服务器中获得的请求有不同的协议。 (我从 express 获取请求并通过上下文将其传递给 GraphQL 解析器)
  3. 当然还有如何将 cookie 发送到不同域上的客户端。

如有任何帮助,我将不胜感激。

问题出在 Heroku 的代理上。我必须将以下内容添加到快速服务器:

 app.set("trust proxy", 1);