【问题标题】:connect.sid not getting stored in cookieconnect.sid 未存储在 cookie 中
【发布时间】:2022-01-08 12:48:15
【问题描述】:

我有一个 node express 应用程序。


const session = require('express-session');
const config = require('config');
var MemoryStore = require('memorystore')(session);


module.exports = function (app) {
    app.use(express.json());
    app.use(
        session({
            saveUninitialized: false,
            cookie: { maxAge: 86400000 },
            store: new MemoryStore({
                checkPeriod: 86400000
            }),
            resave: false,
            secret: config.get('Storagehash')
        })
    );
    app.use('/api/auth', users);
}


我已经分离了身份验证路由并将其放在一个单独的文件中,就像这样。当我执行 console.log(req.session) 时,我得到了正确的输出。


const router = express.Router();


router.post('/', async (req, res) => {

....
    req.session.isAuth = true;    
    console.log(req.session);
    req.session.customerID = customer;
 
    res.send(token);


}

但是当我查看 cookie 选项卡时,connect.sid 没有插入那里。

【问题讨论】:

    标签: node.js express session-cookies


    【解决方案1】:

    您有前端应用程序吗?如果是这样,每当您向后端发送请求时,您都需要在请求中包含withCredentials: true。这会将 cookie 发送到您的后端。如果您使用 axios 发出请求,可以这样完成:

    (假设你的端口是 5000) axios.post("http://localhost:5000/api/auth/", {}, { withCredentials: true });

    【讨论】:

      猜你喜欢
      • 2014-01-25
      • 1970-01-01
      • 2023-03-21
      • 2017-11-03
      • 2022-10-06
      • 2016-10-30
      • 2020-05-05
      • 2021-06-29
      • 2021-06-04
      相关资源
      最近更新 更多