After successfully logging in, I am trying to redirect to another page but keep encountering the error message "Cannot set headers after they are sent to the client". I understand that I need to place the res.redirect method somewhere else in my code, but I am struggling to figure out the right placement.
router.get('/login',(req,res)=>{
res.render('login')
})
router.post('/login',(req,res)=>{
user.findOne({
where: {
userName : req.body.userName
}
})
.then(userInfo=>{
if(userInfo){
if(bcrypt.compareSync(req.body.password,userInfo.password)){
const token = jwt.sign(userInfo.dataValues,process.env.SECRET_KEY,{
expiresIn:1440
})
res.send(token)
res.redirect('/home')
}
else {
res.status(400).json({error:'user doesnt exist'})
}
}
}
)
})