我在Heroku中同时托管了前端和后端。
我正在尝试实现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。
请看下面的图片。
我在这里看不到任何状态代码,但它说它是失败的。这些都是我可以看到的cookie信息,但如果我通过应用程序选项卡检查,它是不可用的。
我在这里遗漏了什么?谁能帮帮我?
可能你应该尝试安全的。secure: req.secure || req.headers['x-forwarded-proto'] === 'https'