I am working on sending a reset token via email to a potential user in Node.js, Sequelize, and Express.
The code works, but I still think it could be improved by handling the promises better.
exports.sendResetToken = function(req,res) {
const username = req.headers.username;
User.findOne({
where: {username: username}
}).then((user) => {
if(user == null){
res.sendStatus(404);
} else {
let token = user.generateResetToken();
user.update({
resetToken: token,
}).then((user) => {
email.sendResetToken(user.email,user.resetToken);
res.sendStatus(200);
}).catch((err) => {
console.error(err);
res.sendStatus(400);
})
}
return null;
}).catch((err) => {
console.error(err);
res.sendStatus(401);
})
}