Does anyone know why after I login on localhost it works perfectly fine, but when I deploy it on heroku netlify, it's having trouble logging in
Short YT vid: https://youtu.be/lpyJo6tmiRs
Login
app.post('/login', async (req, res) => {
const email = req.body.email;
const password = req.body.password;
voterModel.find({email: email}, {"email":1}, async (err, result) => {
if (err) {
console.log(err)
} else {
if(result.length > 0) {
const user = await voterModel.findOne({email: email})
const pass = await user.comparePassword(password)
if (pass) {
req.session.voter = result
var oneWeek = 60 * 60 * 24; //1 weeks
req.session.voter.expires = new Date(Date.now() oneWeek);
req.session.voter.maxAge = oneWeek;
console.log(req.session.voter)
res.send(result)
} else {
console.log("NOT LOGGED IN")
res.send({ message: 'Invalid email or password!'})
}
} else {
console.log("NOT LOGGED IN")
res.send({ message: 'Invalid email or password!'})
}
}
})
})
code for reading the session data After I log in, it goes to the condition else, instead going to loggedIn: true after I logged in
app.get('/login', async (req, res) => {
if (await req.session.voter) {
res.send({loggedIn: true, user: await req.session.voter})
} else {
res.send({loggedIn: false})
}
})
session
app.use(session({
key: "userId",
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
}))
CodePudding user response:
I fixed it, I referred to this post: Express-Session not working in production/deployment
I was missing some configuration on my express session that I need to setup in order to work properly on heroku