无法设置Heroku上的快递应用的cookie。

问题描述 投票:1回答:1

我在Heroku中同时托管了前端和后端。

  • 前端 - xxxxxx.herokuapp.com (react app)
  • 后台 - yyyyy.herokuapp.com (快递)

我正在尝试实现Google认证。在从Google OAuth2中获取令牌后,我试图将其设置为 id_token 和用户的详细信息通过快递应用在cookie中。

下面是我在后台的一段代码。

authRouter.get('/token', async (req, res) => {
    try {
        const result = await getToken(String(req.query.code))
        const { id_token, userId, name, exp } = result;
        const cookieConfig = { domain: '.herokuapp.com', expires: new Date(exp * 1000), secure: true }
        res.status(201)
            .cookie('auth_token', id_token, {
                httpOnly: true,
                ...cookieConfig

            })
            .cookie('user_id', userId, cookieConfig)
            .cookie('user_name', name, cookieConfig)
            .send("Login succeeded")
    } catch (err) {
        res.status(401).send("Login failed");
    }
});

我在本地的工作很完美,但在heroku上却不行。

这些是我已经试过的域名--------。.herokuapp.com herokuapp.com. 另外,我试了一下,没有指定域字段本身。

我可以看到 Set-Cookie 响应头的详细信息,但令牌端点失败,没有返回任何状态代码,而且我无法在应用程序选项卡上看到设置的cookie。

请看下面的图片。

enter image description here我在这里看不到任何状态代码,但它说它是失败的。I can't see any status code here这些都是我可以看到的cookie信息,但如果我通过应用程序选项卡检查,它是不可用的。enter image description here

我在这里遗漏了什么?谁能帮帮我?

node.js express heroku cookies setcookie
1个回答
0
投票

可能你应该尝试安全的。secure: req.secure || req.headers['x-forwarded-proto'] === 'https'

© www.soinside.com 2019 - 2024. All rights reserved.