I was writing an API to send forgotten password emails. But the email I am sending is going to the promotion tab. But I don't want to send emails in the promotion tab.
Sendinblue Config Configure the sendinblue.
const Sib = require("sib-api-v3-sdk");
const client = Sib.ApiClient.instance;
const apiKey = client.authentications["api-key"];
apiKey.apiKey = process.env.SENDINBLUE_API_KEY;
const tranEmailApi = new Sib.TransactionalEmailsApi();
API Code This API finds the user on the database, generates a jwt token that expires in 10 minutes, and sends this to the email.
exports.forgotPassword = (req, res) => {
const { email } = req.body;
User.findOne({ email }).exec((error, user) => {
if (error || !user) {
return res
.status(400)
.json({ error: "User with this email does not exists" });
} else {
const token = jwt.sign(
{ _id: user._id },
process.env.PASSWORD_RESET_JWT_SECRET,
{ expiresIn: "10m" }
);
const otp = Math.floor(100000 + Math.random() * 900000);
const emailData = {
sender: {
email: "bhalomart@gmail.com",
name: "Bhalo Mart",
},
to: [
{
email: req.body.email,
},
],
subject: `${otp} is your password reset code`,
textContent: `Change Password for account - ${req.body.email}. We have got a request to reset your password. Type the code given below.`,
htmlContent: `<div>
<h1 style="text-align:left;background:transparent">Bhalo Mart</h1>
<h3>Hi. Change password for email - ${req.body.email}</h3>
<p>We have got a request to reset your password. Type the code below</p>
</div>
<div>
<p style="font-weight:bold;font-size:30px">${otp}</p>
</div>
`,
};
user
.updateOne({
"otp.resetPassword.token": token,
"otp.resetPassword.otp": otp,
})
.exec((err, success) => {
if (err)
return res.status(400).json({ error: "Reset password error" });
else {
tranEmailApi
.sendTransacEmail(emailData)
.then(() => {
return res.status(200).json({
message: "Email has sent, please enter the otp",
token: token,
});
})
.catch((error) =>
res.status(400).json({ error: "Something wen't wrong", error })
);
}
});
}
});
};