不确定为什么在使用express会话时没有将cookie发送回我的localhost客户端

tsm1rwdh  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(249)

节点有问题 express-session 基本上,我只是想了解它是如何与我的postgres数据库中的Cookie和会话存储相关的。
首先,我不确定为什么在运行react应用程序的chrome浏览器中没有收到会话id cookie localhost:3000 . 如果我叫路线 localhost:5000/login 从postman收到cookie,但从chrome调用同一路线时: localhost:5000/login 然后检查我的cookies,使用 fetch 应用程序编程接口。
会话在postgres中创建得很好。
以下是我的会话设置中间件:

app.use(session({
  store: new pgSession({
    pool : pool,                // Connection pool
    schemaName: 'my_schema',
    tableName : 'user_sessions'       
  }),  
  secret: randomString.generate({
    length: 14,
    charset: 'alphanumeric'
  }),
  resave: false,
  saveUninitialized: false,
  cookie: { maxAge: 1000 * 60 * 60 * 24,
            httpOnly: true }  
}))

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  next();
});

我的另一个问题是,在我的react应用程序中,我如何使用postgres db中的会话信息来检查所有路由的所有请求是否仍然来自客户端的同一用户?

vfwfrxfs

vfwfrxfs1#

试试这个,改变你的cors中间件

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

并在您提出请求时添加此项

withCredentials: true,
   credentials: "include",

我也有同样的问题,我在 Postman 发帖时收到了烹饪,但在使用axios发出请求时没有收到来自浏览器的烹饪,希望它也适用于您

相关问题