integradted email engine
This commit is contained in:
@ -1,24 +1,29 @@
|
||||
const db = require('../config/dbConfig');
|
||||
var db, transporter = require('../config/dbConfig');
|
||||
|
||||
const mailOptions = {
|
||||
from: 'your-email@example.com', // Replace with your email address
|
||||
to: 'recipient@example.com', // Replace with the recipient's email address
|
||||
subject: 'OTP to login',
|
||||
text: 'Body of the email'
|
||||
};
|
||||
|
||||
exports.sendOtp = (req, res) => {
|
||||
const email = req.body.email;
|
||||
const otp = generateRandom4DigitNumber();
|
||||
try {
|
||||
db.query(
|
||||
'INSERT INTO email_otp (email, otp) VALUES (?, ?) ON DUPLICATE KEY UPDATE otp = ?',
|
||||
[email, otp, otp]
|
||||
);
|
||||
} catch (error) {
|
||||
|
||||
const errorMessage = {
|
||||
error: true,
|
||||
message: error.message,
|
||||
};
|
||||
return errorMessage;
|
||||
}
|
||||
db.query(
|
||||
'INSERT INTO email_otp (email, otp) VALUES (?, ?) ON DUPLICATE KEY UPDATE otp = ?',
|
||||
[email, otp, otp],
|
||||
(err, results) => {
|
||||
if (err) {
|
||||
console.error('Error executing query:', err);
|
||||
res.status(500).json({ success: false, message: 'Internal Server Error' });
|
||||
return;
|
||||
}
|
||||
sendMail(otp, email, req);
|
||||
}
|
||||
);
|
||||
|
||||
// send an email using api
|
||||
res.json({ success: true, message: 'You must have received an email with otp successfully.' });
|
||||
}
|
||||
|
||||
exports.validateOtp = async (req, res) => {
|
||||
@ -43,6 +48,23 @@ exports.validateOtp = async (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
function sendMail(otp, email, req) {
|
||||
const mailOptions = {
|
||||
from: 'communityrule@medlab.host', // Replace with your email address
|
||||
to: email, // Replace with the recipient's email address
|
||||
subject: 'Logging code for community rule',
|
||||
text: 'Code for logging into commnunity rule is ' + otp,
|
||||
};
|
||||
transporter.sendMail(mailOptions, (error, info) => {
|
||||
if (error) {
|
||||
console.error('Error:', error);
|
||||
req.status(500).json({ success: false, message: 'Internal Server Error' });
|
||||
return;
|
||||
}
|
||||
res.json({ success: true, message: 'You must have received an email with otp successfully.' });
|
||||
});
|
||||
|
||||
}
|
||||
function generateRandom4DigitNumber() {
|
||||
// Generate a random number between 1000 and 9999
|
||||
const random4DigitNumber = Math.floor(Math.random() * 9000) + 1000;
|
||||
|
Reference in New Issue
Block a user