i want to do something like when a user searches a mongoDB Database, and user exists it should then send an email to the user with a password Reset link. For some reason it does not work as expected. it just goes ahead to send emails, And does not check if the email exists , i dont know why it behaves so.
My code looks like this :
exports.forgot_pass = function(req,res,next){
User.findOne({
user_email : req.body.user_email
}).exec((err,user)=>{
if(err){
res.status(500).send({message : err});
return;
}
if(!user){
res.status(400).send({message: "Sorry Email does not Exist!"});
}else{
var user_email = req.body.user_email;
const transporter = nodemailer.createTransport({
service:'gmail',
host: 'smtp.gmail.com',
port:'587',
auth:{
user: '**************@gmail.com',
pass: '***********'
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});
const mailOptions = {
from :'***********@gmail.com',
to: user_email,
subject: 'Please Reset your Password',
html : '<h3>Dear User</h3><p>You have requested to Reset your password. To Reset your password Successfully, Follow the Link bellow to Reset it</p><p>Click <a href="https://**********/user/resetPassword.jsp">https://onepercentsoft.oxygen.com/user/resetPassword.jsp</a></p><p>This Email is subject to mandatory instruction.</p><p>Regards,</p><p>Online Service</p>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error)throw error;
return res.send({error:false, data: info, message: 'OK'});
})
}
});
};
But this does not check for anything, It just goes ahead and sends the email. I need help here.
CodePudding user response:
Try to use async await
. The problem may be related on how you are handling promises.
exports.forgot_pass = async function (req, res) {
try {
const { user_email } = req.body;
const user = await User.findOne({ user_email });
if (!user) {
return res.status(400).send({ message: 'Sorry Email does not Exist!' });
}
const transporter = nodemailer.createTransport({
service: 'gmail',
host: 'smtp.gmail.com',
port: '587',
auth: {
user: '**************@gmail.com',
pass: '***********',
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false,
},
});
const mailOptions = {
from: '***********@gmail.com',
to: user_email,
subject: 'Please Reset your Password',
html: '<h3>Dear User</h3><p>You have requested to Reset your password. To Reset your password Successfully, Follow the Link bellow to Reset it</p><p>Click <a href="https://**********/user/resetPassword.jsp">https://onepercentsoft.oxygen.com/user/resetPassword.jsp</a></p><p>This Email is subject to mandatory instruction.</p><p>Regards,</p><p>Online Service</p>',
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) throw error;
return res.send({ error: false, data: info, message: 'OK' });
});
} catch (err) {
res.status(500).send({ message: err });
}
};
CodePudding user response:
Sending email will take a little bit time. first of all you have to check is email exist. if exist then you have to send mail. for this purpose make your function asynchronous and us e await.
exports.forgot_pass = async function(req,res,next){
await User.findOne({
user_email : req.body.user_email
}).exec((err,user)=>{
if(err){
res.status(500).send({message : err});
return;
}
if(!user){
res.status(400).send({message: "Sorry Email does not Exist!"});
}else{
var user_email = req.body.user_email;
const transporter = nodemailer.createTransport({
service:'gmail',
host: 'smtp.gmail.com',
port:'587',
auth:{
user: '**************@gmail.com',
pass: '***********'
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});
const mailOptions = {
from :'***********@gmail.com',
to: user_email,
subject: 'Please Reset your Password',
html : '<h3>Dear User</h3><p>You have requested to Reset your password. To Reset your password Successfully, Follow the Link bellow to Reset it</p><p>Click <a href="https://**********/user/resetPassword.jsp">https://onepercentsoft.oxygen.com/user/resetPassword.jsp</a></p><p>This Email is subject to mandatory instruction.</p><p>Regards,</p><p>Online Service</p>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error)throw error;
return res.send({error:false, data: info, message: 'OK'});
})
}
});
};