How can I find the session Id when using express / connect and a session store?

前端 未结 4 572
星月不相逢
星月不相逢 2021-01-31 16:55

If a user is already logged in and tries to login again in a new instance I\'d like it to log out the other user instance. I don\'t want the same user to be logged in twice on m

4条回答
  •  孤独总比滥情好
    2021-01-31 17:06

    For recent readers;

    Connect middlewares are not included in Express since version 4.

    So in order to have req.sessionID work you must do following:

    1. Make sure you have cookie-parser abd express-session modules inside your package.json. If it's not added, add them:
    npm install express-session --save
    npm install cookie-parser --save
    
    1. Be careful about the order while requiring them in your app.js file and add required configuration parameters.
    var cookieParser = require('cookie-parser');
    var session = require('express-session')
    app.use(cookieParser());
    app.use(session({
        secret: '34SDgsdgspxxxxxxxdfsG', // just a long random string
        resave: false,
        saveUninitialized: true
    }));
    
    1. Now you should be using req.sessionID and req.session.id.

提交回复
热议问题