I am trying to create an authentication system using expressjs and implementing session management using express-session.
On the login route, after validating email and password, I am creating a new sessionID for the user using req.session.regenerate, but the callback function inside regenrate is being called two times,although it should be called only one time.
Code:
route.post("/login", async (req, res, next) => {
//validate email and password
//get user object from database
req.session.regenerate(function(err){
req.session.uid = user.id
req.session.loginTime = new Date()
req.session.lastActive = new Date()
res.sendStatus(200)
})
return
});
In the above code for some reason the callback inside session.regenerate is called multiple times(twice) instead of once, so that res.sendStatus and throws the error
(node:10296) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at ServerResponse.header (C:\Users\Lenovo\DATA\college\FCS\ecstacy\api\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (C:\Users\Lenovo\DATA\college\FCS\ecstacy\api\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\Lenovo\DATA\college\FCS\ecstacy\api\node_modules\express\lib\response.js:267:15)
at C:\Users\Lenovo\DATA\college\FCS\ecstacy\api\src\main.ts:40:7
at Generator.next (<anonymous>)
at fulfilled (C:\Users\Lenovo\DATA\college\FCS\ecstacy\api\src\main.ts:5:58)