Skip to content

Commit dfb39d5

Browse files
committed
set validity duration for email link
1 parent deb83e0 commit dfb39d5

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

backend/controllers/getControllers/sendVerificationEmail.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@ const crypto = require("crypto");
22
const User = require("../../models/userModal");
33
const sendEmail = require("../../utils/smtpTransport");
44
const encrypt = (text) => {
5-
var cipher = crypto.createCipher(process.env.ENCRYPTION_ALGO, process.env.CRYPTOJS_SECRET);
5+
var cipher = crypto.createCipher(
6+
process.env.ENCRYPTION_ALGO,
7+
process.env.CRYPTOJS_SECRET
8+
);
69
var encrypted = cipher.update(text, "utf8", "hex") + cipher.final("hex");
7-
// var decipher = crypto.createDecipher(algorithm, key);
8-
// var decrypted = decipher.update(encrypted, "hex", "utf8") + decipher.final("utf8");
910
return encrypted;
1011
};
1112
const sendVerificationEmail = async (req, res) => {
1213
const id = req.session.userId;
14+
//check if email is already verified
1315
const user = await User.findById(id).exec();
1416
if (user.emailVerified)
1517
return res.status(400).send({ error: "Your email is already verified" });
18+
//encrypt the id and current time
1619
var encryptedID = encrypt(id);
17-
const host = req.get("host");
18-
req.session.host = host; //to use while verifying through provided link
19-
const protocol = req.protocol;
20-
req.session.protocol = protocol; //to use while verifying email through provided link
21-
const link = `${protocol}://${host}/verifyEmail?id=${encryptedID}`;
20+
var encryptedTime = encrypt(date.now());
21+
//construct link with the encrypted ID and current time
22+
//a = id && b = time, used bad naming so that verification link do not get very obivious to user or third party
23+
const link = `${process.env.PROTOCOL}://${process.env.HOST}/verifyEmail?a=${encryptedID}&b=${encryptedTime}`;
24+
//create the email
2225
const mailOptions = {
2326
to: user.email,
2427
subject: "Please confirm your Email Address",
25-
html: `Hello,<br> Please Click on the link to verify your email.<br><a href=${link}>Click here to verify</a>`,
28+
html: `<img src="https://media-exp1.licdn.com/dms/image/sync/C4E22AQG4zTDlyguaNQ/feedshare-shrink_800/0/1602572271436?e=1623888000&v=beta&t=Aq2jU_LLoM8lK5Mq7TgYxdf-9tkVZJs3Bc2m0HK68tw" /><br><br>Hello,<br> Please Click on the link to verify your email.<br><a href=${link}>Click here to verify</a><br>The above link is valid for ${process.env.EMAIL_LINK_VALIDITY} minutes only`,
2629
};
2730
try {
31+
//send email to the user
2832
let response = await sendEmail(mailOptions);
2933
console.log(response);
30-
// return res
31-
// .status(200)
32-
// .send({
33-
// msg: "An email has been sent to your email address. Click on the provided link to verify your email address",
34-
// });
35-
return res.render("verifyEmail");
34+
//render verifyEmail page to inform user that an email has been sent at his registered mobile number now
35+
return res.render("verifyEmailMsg");
3636
} catch (err) {
3737
console.log(err);
3838
return res.status(500).send(err);
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
const crypto = require("crypto");
22
const User = require("../../models/userModal");
3-
const decrypt = (cipher) => {
4-
const decipher = crypto.createDecipher(process.env.ENCRYPTION_ALGO, process.env.CRYPTOJS_SECRET);
5-
var decrypted = decipher.update(cipher, "hex", "utf8") + decipher.final("utf8");
3+
const decrypt = (cipher) => {
4+
const decipher = crypto.createDecipher(
5+
process.env.ENCRYPTION_ALGO,
6+
process.env.CRYPTOJS_SECRET
7+
);
8+
var decrypted =
9+
decipher.update(cipher, "hex", "utf8") + decipher.final("utf8");
610
return decrypted;
7-
}
11+
};
812
const verifyEmail = async (req, res) => {
9-
const host = req.get('host');
10-
if(`${req.protocol}://${host}` == `${req.session.protocol}://${req.session.host}`){//domain matched
11-
//now we don't need protocol and host in our session variables;
12-
req.session.protocol = null;
13-
req.session.host = null;
14-
const encryptedID = req.query.id;
13+
const host = req.get("host");
14+
if (
15+
`${req.protocol}://${host}` ==
16+
`${process.env.PROTOCOL}://${process.env.HOST}`
17+
) {
18+
//domain matched
19+
const encryptedID = req.query.a; //since a = id
20+
const encryptedTime = req.query.b; //since b = time
1521
console.log(encryptedID);
16-
// const decryptedBytes = CryptoJS.AES.decrypt(
17-
// encryptedID,
18-
// process.env.CRYPTOJS_SECRET
19-
// );
20-
// console.log(decryptedBytes);
21-
// const decryptedID = decryptedBytes.toString(CryptoJS.enc.Utf8);
22+
console.log(encryptedTime);
23+
//decrypt ID and Time
2224
const decryptedID = decrypt(encryptedID);
23-
console.log("decryptedID : ", decryptedID);
25+
const decryptedTime = decrypt(encryptedTime);
26+
console.log("decryptedID : ",decryptedID," decryptedTime : ",decryptedTime);
2427
const user = await User.findById(decryptedID).exec();
2528
if (user) {
29+
const timeElapsed = (Date.now() - decryptedTime) / (1000 * 60); //in minutes;
2630
console.log(user);
27-
user.emailVerified = true;
28-
user.save();
29-
console.log("email verified");
30-
res.redirect('home');
31-
}else{
32-
res.status(400).send({error:"Invalid request!"});
31+
if (timeElapsed < process.env.EMAIL_LINK_VALIDITY) {
32+
//email link is clicked in valid time duration
33+
user.emailVerified = true;
34+
user.save();
35+
console.log("email verified");
36+
res.redirect("home");
37+
} else {
38+
//link not clicked in valid time duration
39+
res.render("sentEmailLinkAgain");
40+
}
41+
} else {
42+
res.status(400).send({ error: "Invalid request!" });
3343
}
34-
}else{
35-
//domain didn't matched
36-
//now we need to reset req.session.host, req.session.protocol
37-
req.session.protocol = null;
38-
req.session.host = null;
39-
res.status(400).send({error:"Invalid request!"});
44+
} else {
45+
//domain didn't matched
46+
//now we need to reset req.session.host, req.session.protocol
47+
req.session.protocol = null;
48+
req.session.host = null;
49+
res.status(400).send({ error: "Invalid request!" });
4050
}
4151
};
4252
module.exports = verifyEmail;

0 commit comments

Comments
 (0)