-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = ` | ||
<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> | ||
|
@@ -31,17 +31,16 @@ const otpMessage = (otp, timeLeft)=> { | |
</div> | ||
</div> | ||
`; | ||
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:'[email protected]', //This should be in environement variable | ||
from: '[email protected]', //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','[email protected]') | ||
//call this function to test controller | ||
// sendUserOtp('1','[email protected]') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
const response = (success, message, data) => { | ||
return { | ||
success: success, | ||
message: message, | ||
data: data | ||
} | ||
} | ||
const response = (success, message, data) => ({ | ||
success: success, | ||
message: message, | ||
data: data, | ||
}); | ||
|
||
module.exports = (response) | ||
module.exports = response; |