[英]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.