I have started using express-session. But I am not receiving any cookie in response. Here is my code:
app.use(session({
secret: 'jdfgghuheghgjbfhfguhrughhdjdjhjghj',
saveUninitialized: false,
resave: false
}))
app.post('/api/login', async (req, res) => {
const {email, password} = req.body
//console.log(req.body)
console.log(email, password)
const resp = await User.findOne({email,password})
// console.log(resp)
if(!resp) {
// console.log("incorrect details")
res.json({
success: false,
message: "Incorrect details"
})
// user login is incorrect
} else {
res.json({
success: true
})
req.session.user = email
req.session.save()
console.log("logging you in")
//make a session and set user to logged in.
}
})
Trying to get the user here:
app.get('/api/data', (req, res) => {
console.log(req.session)
res.send('User is =>' + req.session.user)
})
I should get a set-cookie in response header in the network tab. Can anyone please tell me where am I missing out?