Im trying to build a simple login system but when i try the asign the session i get this error
Cannot set property 'user' of undefined
However this is my code:
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,}))
This is the login controller:
const loginModel = require('../model/loginModel');
module.exports = {
login : async function(req,res){
try {
const query = await loginModel.login(req.body.email,req.body.password);
if(query != null){
req.session.user = query;
res.status(200).json(req.session.user);
}else{
res.status(401).json({error : true});
}
} catch (error) {
console.error(error.message);
res.status(500).send('something went wrong');
}
}
}
NOTE : login model is 100% working and its retrieving the user from database
express-sessionmiddleware before this request? That is required to create thereq.sessionobject and to tie it to a cookie.req.sessiondoes not appear to exist based on the error you are getting. Are the routes declared in the right order (with the session middleware first). Perhaps you should show us much more of your code so we can see where these two things fit relative to one another. It's also possible that you're passing the wrong arguments to thelogin()function. We can't see what the problem is without seeing more code.