React & Node:Cookie 因域无效而被拒绝

React & Node: Cookie has been rejected for invalid domain

我已经使用 Node 作为服务器并使用 Next 作为 front-end 创建了一个网站。当我登录时,我使用这些 headers:

发出请求
access-control-allow-credentials: true
access-control-allow-origin: https://loop.herbievine.com
server: nginx
set-cookie: lid=s%3AF4sw6csUWeU-nytZoR5w6nrkR_4gbpd2.zt5brbuuedQiyurZOB4Hn%2FSpRa8JqtrlYhY5AHxs9Yk; Domain=https://loop.herbievine.com; Path=/; Expires=Thu, 01 Apr 2021 15:11:56 GMT; HttpOnly; Secure; SameSite=Lax
strict-transport-security: max-age=15724800; includeSubdomains
vary: Origin
x-powered-by: Express

看到我得到 set-cookie header 回复。在控制台中,我看到 Cookie “lid” has been rejected for invalid domain.

在我的服务器上,我用 redis 初始化 cookie,如下所示:

session({
    name: COOKIE_NAME,
    store: new Store({
        client: redis,
        disableTouch: true
    }),
    cookie: {
        httpOnly: true,
        secure: process.env.NODE_ENV === 'production',
        sameSite: 'lax',
        maxAge: 1000 * 60 * 60 * 24 * 7, // 1 week
        domain: process.env.CORS_ORIGIN // this being set to https://loop.herbievine.com
    },
    saveUninitialized: false,
    secret: process.env.SESSION_SECRET,
    resave: false
})

那为什么会失败呢?是否有其他设置可供使用?

顺便说一句,我使用 Vercel + Cloudflare 进行所有 DNS 设置,服务器是 运行 在 VPS + dokku on Digital Ocean

问题似乎出在您使用 url 而不是域。您应该使用域 loop.herbievine.com 而不是 https://loop.herbievine.com.

如果您想与其他子域共享 cookie,那么您将使用该域作为 .herbievine.com