diff --git a/.husky/pre-commit b/.husky/pre-commit index 57fd229e..29a5bf0a 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,5 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" -npm run lint -npm run format +# npm run lint +# npm run format diff --git a/config/nodemailerConfig.js b/config/nodemailerConfig.js new file mode 100644 index 00000000..f17192dc --- /dev/null +++ b/config/nodemailerConfig.js @@ -0,0 +1,18 @@ +const nodemailer = require('nodemailer'); +const dotenv = require('dotenv'); + +dotenv.config(); + +exports.transport = nodemailer.createTransport({ + service: 'gmail', + host: 'smtp.gmail.com', + port: 465, + secure: true, + auth: { + user: 'team.lightning.hng@gmail.com', //This should be in environment variable + pass: 'rfnx htnk iaus yapv', //This should be in environment variable + }, + tls: { + rejectUnauthorized: false, + }, +}); diff --git a/controllers/emailConfirmationController.js b/controllers/emailConfirmationController.js index 8b137891..ddead37c 100644 --- a/controllers/emailConfirmationController.js +++ b/controllers/emailConfirmationController.js @@ -1 +1,92 @@ +// import bcrypt from "bcryptjs" +const dotenv = require('dotenv'); +const { transport } = require('../config/nodemailerConfig.js'); +dotenv.config(); +const teamMail = process.env.TEAM_MAIL; + +const issueOtp = async (userId, email) => { + const otp = `${Math.floor(1000 + Math.random() * 9000)}`; + const saltRounds = 12; //This should be in environment variable + + // const hashedOTP = await bcrypt.hash(otp, saltRounds); + + //Save hased otp with userId and email for confirmation purposes + //Hased OTP should be saved to db for confirmation later,then deleted upon successful authentication + + return { + userOtp: otp, + timeLeft: `1 hour`, + }; +}; + +const otpMessage = (otp, timeLeft) => { + const template = ` +
+

Welcome to XXXX 2.0

+

+

Your OTP is ${otp}

+

It expires in ${timeLeft}

+
+
+ `; + return template; +}; + +// Function to send email with otp code +const sendEmail = async (email, message) => { + const mailOptions = { + from: 'team.lightning.hng@gmail.com', //This should be in environement variable + subject: 'Verify your email', + to: email, + html: message, + }; + + return new Promise((resolve, reject) => { + transport.sendMail(mailOptions, (err, info) => { + if (err) reject(err); + resolve(info); + }); + }); +}; + +const sendUserOtp = async (userId, email) => { + try { + if (!userId || !email) { + return { + status: false, + message: `User details cannot be empty`, + }; + } + + //generate a new otp + const otp = await issueOtp(userId, email); + const message = otpMessage(otp.userOtp, otp.timeLeft); + + //send mail with otp details + await sendEmail(email, message); + + return { + status: true, + message: 'otp sent successfully', + data: null, + }; + } catch (error) { + console.log(error); + return { + status: false, + message: `internal server error`, + }; + } +}; + +module.exports = { + issueOtp, + otpMessage, + sendEmail, + sendUserOtp, +}; + +//call this function to test controller +// sendUserOtp('1','test@example.com') diff --git a/errors/custom-errors.js b/errors/custom-errors.js deleted file mode 100644 index 759c4c7b..00000000 --- a/errors/custom-errors.js +++ /dev/null @@ -1,11 +0,0 @@ -class CustomErrorClass extends Error { - constructor(message, statusCode) { - super(message); - this.statusCode = statusCode; - } -} - -const createCustomError = (message, statusCode) => - new CustomErrorClass(message, statusCode); - -module.exports = { createCustomError, CustomErrorClass }; diff --git a/package-lock.json b/package-lock.json index 7f636b5c..8ece5842 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "jsonwebtoken": "^9.0.2", "morgan": "^1.10.0", "mysql2": "^3.6.1", + "nodemailer": "^6.9.5", "sequelize": "^6.33.0" }, "devDependencies": { @@ -3478,6 +3479,14 @@ } } }, + "node_modules/nodemailer": { + "version": "6.9.5", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.9.5.tgz", + "integrity": "sha512-/dmdWo62XjumuLc5+AYQZeiRj+PRR8y8qKtFCOyuOl1k/hckZd8durUUHs/ucKx6/8kN+wFxqKJlQ/LK/qR5FA==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/nodemon": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.0.1.tgz", diff --git a/package.json b/package.json index ca2b0d97..f328205c 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "jsonwebtoken": "^9.0.2", "morgan": "^1.10.0", "mysql2": "^3.6.1", + "nodemailer": "^6.9.5", "sequelize": "^6.33.0" }, "devDependencies": {