Answer the question
In order to leave comments, you need to log in
How to send verification code by mail?
How can I pass the verification code so that it is generated when sending a message and I can receive it on the client?
const nodemailer = require("nodemailer");
const responseCreator = require("../response_creators/ResponseCreator");
const UserError = require("../error/UserError");
const generateCode = (function () {
code = Math.round(Math.random() * 1000000)
})
const verificationCode = generateCode()
const getEmailData = (to) => {
const message = {
to,
subject: "BorrowLand: Verification code",
html: `
<h2>Verification code</h2>
<ul>
<li>${verificationCode}</li>
</ul>`,
};
return message;
};
const sendEmail = (to) => {
const transporter = nodemailer.createTransport(
{
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: "*****",
pass: "****",
},
},
{
from: "Borrow Land <[email protected]>",
}
);
const mail = getEmailData(to);
transporter.sendMail(mail, function (error, response, next) {
if (error) {
return next(UserError.badRequest(error));
} else {
return response.json(responseCreator.response({message: "email send"}));
}
transporter.close();
});
};
class VerificationCodeController{
async sendCode(req, res){
const sendEmailCode = await sendEmail(req.body.email)
return res.json(responseCreator.response(sendEmailCode));
}
async recieveCode(req, res){
return res.json(responseCreator.response({verificationCode}))
}
}
module.exports = new VerificationCodeController();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question