Skip to content

Commit

Permalink
Revert "Added config "
Browse files Browse the repository at this point in the history
  • Loading branch information
ExtranoDev authored Sep 21, 2023
1 parent 66e81ba commit 5c42044
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 121 deletions.
4 changes: 2 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# npm run lint
# npm run format
npm run lint
npm run format
18 changes: 0 additions & 18 deletions config/nodemailerConfig.js

This file was deleted.

91 changes: 0 additions & 91 deletions controllers/emailConfirmationController.js
Original file line number Diff line number Diff line change
@@ -1,92 +1 @@
// 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 = `
<div style="max-width: 700px;text-align: center;background: #f4f8fd;
margin:auto; border: 10px solid #ddd; padding: 50px 20px; font-size: 110%;">
<h2 style="color: #FF7F50;">Welcome to XXXX 2.0</h2>
<div style="text-align:center ; color:black;"><br>
<h3 style="color: teal;">Your OTP is ${otp}</h3>
<h4 style="color: teal;">It expires in ${timeLeft}</h4>
</div>
</div>
`;
return template;
};

// Function to send email with otp code
const sendEmail = async (email, message) => {
const mailOptions = {
from: '[email protected]', //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','[email protected]')
11 changes: 11 additions & 0 deletions errors/custom-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CustomErrorClass extends Error {
constructor(message, statusCode) {
super(message);
this.statusCode = statusCode;
}
}

const createCustomError = (message, statusCode) =>
new CustomErrorClass(message, statusCode);

module.exports = { createCustomError, CustomErrorClass };
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"jsonwebtoken": "^9.0.2",
"morgan": "^1.10.0",
"mysql2": "^3.6.1",
"nodemailer": "^6.9.5",
"sequelize": "^6.33.0"
},
"devDependencies": {
Expand Down

0 comments on commit 5c42044

Please sign in to comment.