Tuesday, 1 September 2020

Starting session outside of the initial app.js results in undefined session (Express.js)

Helloes,I want to start the session outside of the app.js which is my initial starter file. It does work without problems when starting it in app.js, but when I want to start it outside, it stops working.const system = { async startSession(req) { req.app.use(Session({ store: new MongoStore({mongooseConnection: Mongoose.connection}), genid: (req) => { return uuid() }, name: (req.app.get('env') === 'production') ? '__Host-sid' : 'sid', secret: Process.env.APP_SESSION_SECRET, resave: false, saveUninitialized: true, cookie: { secure: req.app.get('env') === 'production', httpOnly: true, expires: new Date(Date.now() + 3600000), sameSite: 'strict' } })) } } module.exports = system; This is my system.js. I initialize via await System.startSession(req); and when I try to access req.session, I get undefined. No error messages. And if I use the very same code in app.js (with app.use() instead of req.app.use()), everything works fine.

Submitted September 01, 2020 at 10:40PM by samsungfutanari69

No comments:

Post a Comment