简体   繁体   English

不确定为什么在使用 express-session 时没有 cookie 被发送回我的本地主机客户端

[英]Unsure as to why no cookie is being sent back to my localhost client when using express-session

Having issues with node express-session and basically just trying to understand how it all works with regards to cookies and my session store within my postgres database. node express-session有问题,基本上只是想了解它是如何与 cookie 和我的 postgres 数据库中的会话存储相关的。

For starters, I'm not sure why I don't receive a session id cookie within my chrome browser where my react app is running on localhost:3000 .首先,我不知道为什么我的 chrome 浏览器中没有收到 session id cookie,我的 react 应用程序在localhost:3000上运行。 If I call the route localhost:5000/login from postman, a cookie is received but when calling the same route from Chrome: localhost:5000/login and then check my cookies, nothing is created when using the fetch API.如果我从邮递员那里调用路由localhost:5000/login ,则会收到一个 cookie,但是当从 Chrome 调用相同的路由时: localhost:5000/login然后检查我的 cookie,使用fetch API 时不会创建任何内容。

The session is created fine within postgres.会话在 postgres 中创建得很好。

Here is my middleware for session setup:这是我的会话设置中间件:

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();
});

My other question is, within my react app, how can I use the session info within my postgres db to check that all requests to all routes are still coming from the same user on the client side?我的另一个问题是,在我的 React 应用程序中,如何使用我的 postgres 数据库中的会话信息来检查对所有路由的所有请求是否仍然来自客户端的同一用户?

try this , change your cors middleware this试试这个,改变你的 cors 中间件

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

and add this when you make your request并在您提出请求时添加此内容

 withCredentials: true,
   credentials: "include",

I had same problem I was recieving the cooking when making post from postman, but not from the browser when making request with axios, hopefully it works for you too as well我遇到了同样的问题,我在从邮递员发帖时收到了烹饪,但在使用 axios 发出请求时没有从浏览器收到,希望它也适用于你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 cookie 在页面刷新时被删除? 快速会话,redis 并做出反应 - Why is my cookie being deleted on page refresh? express-session, redis and react Express-session 不向本地主机发送安全 cookie - Express-session not sending secure cookie to localhost 使用 express-session 将标头发送到客户端后无法设置标头 - Cannot set headers after they are sent to the client using express-session 在带有 express-session 的 NodeJS 中使用安全 cookie 时,会话不会持久化 - session not persisting when using secure cookie in NodeJS with express-session 为什么我的快速会话不持久? - Why is my express-session not persisting? Node JS,快速会话,从客户端浏览器中删除 cookie - Node JS, express-session, remove cookie from client browser 使用快速会话时身份验证失败 - Authentication failure when using express-session 什么时候我应该使用cookie解析器与快速会话? - when should I use cookie-parser with express-session? 为什么我的 cookies 没有使用 express-session 和 nodejs 在我的应用程序的前端设置? - How come my cookies are not being set in the frontend of my app using express-session and nodejs? 为什么 express-session connect.sid 值在客户端可见? - Why is express-session connect.sid value is visible on client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM