在Local Host上运行客户端时未阅读会话的表达会话
我面临的问题是阅读由用户角色在数据库中设置的会话的角色属性。为什么这种行为发生?它使得开发依赖于API端点的后端控制的新功能很难。
身份验证代码
let role = await userAuth.authUser(username, password)
if(role == null){
res.status(401).send()
}else{
req.session.role = role
req.session.save()
res.json(req.session.role)
}
然后将其存储在我的PSQL DB中的会话表中。 我需要从会话中读取管理角色
exports.getJWT = async (req, res) => {
if (req.session.role[0].role == 'admin') {
console.log("Admin found")
}
对于某些函数,当我从Express Server运行的同一服务器发送该函数时, ,而req.session.role [0] .role
可以读取。但是,当我在Localhost上运行开发环境并向同一后端发送请求时,该属性是未定义的,功能失败了。
这是我的主express应用程序
server.js 的会话代码
const sessionConfig = {
store: new pgSession({
pool: sessionDBaccess,
tableName: 'session'
}),
name: "session",
secret: "SecretsStaySecret",
cookie:{httponly:false,secure: true, sameSite: "none", expires: 12 * 60 * 60 *1000}
}
The problem I am facing is down to reading a role property of a session set by the users role in a database. Why is this behaviour happening? It makes it difficult to develop new features which rely on backend control of API endpoints.
authentication code
let role = await userAuth.authUser(username, password)
if(role == null){
res.status(401).send()
}else{
req.session.role = role
req.session.save()
res.json(req.session.role)
}
This is then stored in a session table in my PSQL db. For some functions I require an admin role to be read from the session
exports.getJWT = async (req, res) => {
if (req.session.role[0].role == 'admin') {
console.log("Admin found")
}
When I send a request to this function from the same server where the express server is running it passes, and the req.session.role[0].role
can be read. However, when I am running a development environment on my localhost and I send a request to the same backend, the property is undefined and the function fails.
Here is my session code from my main express application
server.js
const sessionConfig = {
store: new pgSession({
pool: sessionDBaccess,
tableName: 'session'
}),
name: "session",
secret: "SecretsStaySecret",
cookie:{httponly:false,secure: true, sameSite: "none", expires: 12 * 60 * 60 *1000}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您不使用https set
安全:false
时,请参见更多详细信息: https://jonathan-holloway.medium.com/node-and-express-session-a23eb36a052when you are not using https set
secure:false
, see more detail here:https://jonathan-holloway.medium.com/node-and-express-session-a23eb36a052