CORS:浏览器不会发送或接收Cookie

时间:2020-10-22 10:55:49

标签: javascript reactjs cookies axios cross-domain

我遇到一个问题,即在生产环境中(netlify前端,heroku后端),我的登录cookie没有被发送到浏览器(也没有发送回服务器)。当React和Express在本地运行时,就可以了-我可以看到CORS允许进行请求,并向其发送Cookie。

奇怪的是,在生产中,POSTMAN能够登录并接收Cookie。只是React,在本地运行和部署时,都会向部署的生产服务器发出请求,但不会发送回cookie。 (我在Heroku上的生产env变量设置为“生产”,并且在下面的CORS代码中,起源应确定是这种情况。)

我在Express应用程序中的CORS配置是:

const cors = require('cors');

let origin = 'http://192.168.1.132:3000'

if (process.env.NODE_ENV === 'production') {
   origin = 'https://appname.app'
}

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', origin);
  res.header('Access-Control-Allow-Credentials', true);
  res.header(
    'Access-Control-Allow-Headers',
    'Origin, X-Requested-With, Content-Type, Accept'
);

next();
});

app.use(cors({ origin: origin, credentials: 'true' }));

我在React中的请求如下:

axios
.post(
    `${serverURL}/signin`,
           {
              email: emailInput,
              password: passInput,
              client: 'React',
           },

           {
              withCredentials: true,
              credentials: 'include',
           }
     ).then((response) => {
          // handles response
                
          });

有人在这里看到我做错了什么吗?

0 个答案:

没有答案