From 8ea005b7d935f6ec48091af31bd6528a9389fe3b Mon Sep 17 00:00:00 2001 From: PraiseJah Moses Date: Thu, 21 Sep 2023 17:00:17 +0100 Subject: [PATCH 1/2] Added file changes --- .husky/pre-commit | 4 +- config/nodemailerConfig.js | 18 +++++ controllers/emailConfirmationController.js | 94 +++++++++++----------- package-lock.json | 9 +++ package.json | 1 + 5 files changed, 75 insertions(+), 51 deletions(-) create mode 100644 config/nodemailerConfig.js 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 663758ab..ddead37c 100644 --- a/controllers/emailConfirmationController.js +++ b/controllers/emailConfirmationController.js @@ -1,15 +1,15 @@ // import bcrypt from "bcryptjs" -const { transport } = require("../config/nodemailerConfig.js"); -const dotenv = require("dotenv"); +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 saltRounds = 12; //This should be in environment variable -// const hashedOTP = await bcrypt.hash(otp, saltRounds); + // 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 @@ -20,8 +20,8 @@ const issueOtp = async (userId, email) => { }; }; -const otpMessage = (otp, timeLeft)=> { - let template = ` +const otpMessage = (otp, timeLeft) => { + const template = `

Welcome to XXXX 2.0

@@ -31,17 +31,16 @@ const otpMessage = (otp, timeLeft)=> {
`; - return template; -} - + return template; +}; -// Function to send email with otp code +// 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 + from: 'team.lightning.hng@gmail.com', //This should be in environement variable subject: 'Verify your email', - to:email, - html: message + to: email, + html: message, }; return new Promise((resolve, reject) => { @@ -52,45 +51,42 @@ const sendEmail = async (email, message) => { }); }; - - -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 ({ +const sendUserOtp = async (userId, email) => { + try { + if (!userId || !email) { + return { status: false, - message: `internal server error`, - }); + message: `User details cannot be empty`, + }; } - }; - module.exports ={ - issueOtp, - otpMessage, - sendEmail, - sendUserOtp + //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') \ No newline at end of file +//call this function to test controller +// sendUserOtp('1','test@example.com') 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": { From d3d5c4f93f1c535aed05e1f95af4505abf8be08a Mon Sep 17 00:00:00 2001 From: PraiseJah Moses Date: Thu, 21 Sep 2023 17:11:15 +0100 Subject: [PATCH 2/2] fix --- Errors/custom-errors.js | 12 ------------ errors/custom-errors.js | 11 ----------- 2 files changed, 23 deletions(-) delete mode 100644 Errors/custom-errors.js delete mode 100644 errors/custom-errors.js diff --git a/Errors/custom-errors.js b/Errors/custom-errors.js deleted file mode 100644 index 7cc046c5..00000000 --- a/Errors/custom-errors.js +++ /dev/null @@ -1,12 +0,0 @@ -class CustomErrorClass extends Error { - constructor(message, statusCode) { - super(message) - this.statusCode = statusCode - } -} - -const createCustomError = (message, statusCode) => { - return new CustomErrorClass(message, statusCode) -} - -module.exports = { createCustomError, CustomErrorClass } \ No newline at end of file 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 };