在Local Host上运行客户端时未阅读会话的表达会话

发布于 2025-02-01 12:06:03 字数 1084 浏览 4 评论 0原文

我面临的问题是阅读由用户角色在数据库中设置的会话的角色属性。为什么这种行为发生?它使得开发依赖于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2025-02-08 12:06:03

当您不使用https set 安全:false时,请参见更多详细信息: https://jonathan-holloway.medium.com/node-and-express-session-a23eb36a052

  const sessionConfig = {
  store: new pgSession({
      pool: sessionDBaccess,
      tableName: 'session'
  }),
  name: "session",
  secret: "SecretsStaySecret",
  cookie:{httponly:false,secure: false, sameSite: "none", expires: 12 * 60 * 60 *1000}
}

when you are not using https set secure:false, see more detail here:https://jonathan-holloway.medium.com/node-and-express-session-a23eb36a052

  const sessionConfig = {
  store: new pgSession({
      pool: sessionDBaccess,
      tableName: 'session'
  }),
  name: "session",
  secret: "SecretsStaySecret",
  cookie:{httponly:false,secure: false, sameSite: "none", expires: 12 * 60 * 60 *1000}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文