【问题标题】:Unable to set cookies from backend to frontend when deploying website部署网站时无法从后端设置 cookie 到前端
【发布时间】:2021-12-29 00:37:56
【问题描述】:

我的服务器使用 Nodejs,客户端使用 Reactjs。在我的客户端中,我使用 axios 向服务器执行 post/get 请求。 在开发过程中,一切都运行良好,数据被获取并且 cookie 被正确设置。但是,当我将服务器和客户端分别部署到 Heroku 和 Netlify 时,cookie 突然没有设置。

这是我的服务器代码:


dotenv.config()
const server = express();
server.use(cors({origin: "https://frontendname.com", credentials: true}));

server.use(express.json());
server.use(express.urlencoded({extended:true}))
server.use(cookieParser())
server.use("/", fetchData)

server.listen(process.env.PORT, ()=>console.log(`Server listening on PORT ${process.env.PORT}`))
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true}).then( () => {
    console.log("connected to mongoose")
    

}).catch((error) => console.log(error.message))

我的服务器 API 代码

res.status(201)       .cookie("accessToken", accessToken, {domain:"frontendwebsite.com", httpOnly: true, sameSite: 'strict', path: '/', expires: new Date(new Date().getTime() + 60 * 1000 * 4)})                          .cookie("refreshToken", refreshToken, {domain:"frontendwebsite.com", httpOnly: true, sameSite: 'strict', path: '/', expires: new Date(new Date().getTime() + 60 * 1000 * 96)})                          .json({message: "login verified", username: req.body.username})                 .send()

我的客户端 API 代码:

axios.defaults.withCredentials = true
export const loginAuth = async (credentials) => { 
    return await axios.post("https://backendname.herokuapp.com/loginAuth", credentials).then((res) => {
        
        return res
    })
}


由于“res.cookie”中的域名不正确,我有一种强烈的感觉。因为在我的开发中,当我为服务器和客户端使用 localhost 时,它可以工作。我的客户端托管在 Netlify 上,服务器托管在 Heroku 上。我的客户有一个我从 GoDaddy 获得的自定义域名。我在该域名上使用了 Netlify DNS。有什么想法吗?

【问题讨论】:

    标签: node.js reactjs cookies axios dns


    【解决方案1】:

    您能否检查从服务器返回的响应(使用浏览器开发工具)?是否有任何 Set-Cookie 标头/值返回,还是完全丢失?

    我的猜测是 cookie 标头在那里,并且正在设置,但是将 sameSite 设置为 strict 它不会将其发送到您的后端/API。所以我认为你对域不正确的看法是正确的,在res.cookie 中,你可以尝试使用backendname.herokuapp.com。由于您确实希望 cookie 向/从您的后端传输,特别是因为它是 httpOnly,所以您的前端/客户端永远不会使用它。

    【讨论】:

    • 您好,首先感谢您的回复。从那以后,我尝试了几次更改,但无济于事。我确实收到了 Set-Cookie 标头,其中包含所有选项,例如 httponly、sameSite 和到期时间。但是,我仍然没有在 cookie 选项卡中看到 cookie。还玩弄了域名,不同的是 https、http 和根本没有前缀,以及在域末尾留下斜杠。将sameSite 更改为none,将域更改为我的后端地址也不起作用。有什么想法吗?
    • 由于存在 Set-Cookie 标头,这意味着问题完全出在客户端。我建议转到 Chrome 中的开发工具 -> 控制台,然后单击“问题”按钮,然后选中“包括第三方 cookie 问题”。重新加载后,应显示浏览器尝试设置 cookie 时发生的任何错误。
    • 我想我很接近了。我在 Firefox 上运行它,控制台中的一个错误说由于缺少“安全”选项,cookie 很快就会被拒绝。我将 "secure" 设置为 true ,错误已修复,但仍然无法正常工作。值得注意的一点是,当我将域设置为我的前端地址或后端地址时,会出现一个错误,提示“cookie 已因无效域而被拒绝”。当我完全删除域选项时,错误消失了。但是cookie仍然没有出现。
    • 我认为这是正确的方法,您不需要设置domain,因为默认情况下它将是您从(后端)设置它的域。你也可以删除path 看看会发生什么吗?当您说 cookie 没有出现时,您是否在网络选项卡中查看后续请求(Cookie 标头)?我认为您遇到的下一件事是将SameSite 设置为strict,因为您的前端/后端是不同的域。但是,在您发送请求后,这应该会显示在错误控制台中。 developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/…
    • SameSite 之前设置为“无”,同时将安全设置添加为 true。在网络选项卡中,我仍然在响应中看到 Set-Cookie 标头,只是 Chrome DevTools,在 Application 下,cookie 保持为空。
    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 1970-01-01
    • 2021-02-10
    • 2022-11-14
    • 2021-05-13
    相关资源
    最近更新 更多