From 44132bcfc7db663c6b68481ca3e1bc437526f118 Mon Sep 17 00:00:00 2001 From: Bobby Date: Fri, 24 Jun 2022 22:05:21 +0530 Subject: sending verification emails for email change works --- routes/account.routes.js | 114 +++++++++++++++++++++++++++-------------------- views/account.ejs | 3 -- 2 files changed, 65 insertions(+), 52 deletions(-) diff --git a/routes/account.routes.js b/routes/account.routes.js index d7cbdfc5..16acddac 100644 --- a/routes/account.routes.js +++ b/routes/account.routes.js @@ -5,6 +5,7 @@ const jwt = require("jsonwebtoken"); const connectionString = process.env.DATABASE_URL; const md5 = require("md5"); const { isEmailValid } = require("../functions/validate"); +const nodemailer = require("nodemailer"); router.get("/", (req, res) => { const username = jwt.decode(req.cookies.token).username; @@ -54,8 +55,11 @@ router.post("/sendVerificationEmail", (req, res) => { const username = decoded.username; const newEmail = req.body.email; if (!newEmail || !isEmailValid(newEmail)) { - req.flash("mailsenderror", "Error sending verification email. Provided email is invalid.",); - res.redirect(req.get("referer")); + req.flash( + "mailsenderror", + "Error sending verification email. Provided email is invalid." + ); + res.redirect(req.get("referer")); } else { const connection = mysql.createConnection(connectionString); connection.connect(); @@ -68,53 +72,65 @@ router.post("/sendVerificationEmail", (req, res) => { } else { if (results.length > 0) { const user = results[0]; - // const transporter = require("nodemailer").createTransport({ - // service: "gmail", - // auth: { - // user: process.env.EMAIL_USER, - // pass: process.env.EMAIL_PASSWORD, - // }, - // }); - // // Generate a verification URL and send it to the user - const verificationUrl = `${req.get( - "origin" - )}/verifyemail?token=${jwt.sign( - { - username: user.username, - email: newEmail, - }, - process.env.AUTHORIZATION_STRING, - { - expiresIn: "1h", - } - )}`; - // const mailOptions = { - // from: process.env.EMAIL_USER, - // to: newEmail, - // priority: "high", - // subject: - // "[That Computer Scientist] Request to change your email address", - // html: `

Hi ${user.firstname || user.username},

- //

We received a request to change your email address to ${newEmail}.

- //

If you made this request, please click the link below to verify your new email address:

- //

${verificationUrl}

- //

If you did not make this request, you can ignore this email.

- //

Thanks,

- //

Kumar Priyansh

- //

That Computer Scientist

`, - // }; - // transporter.sendMail(mailOptions, (err, info) => { - // if (err) { - // req.flash("mailsenderror", "Error sending verification email. Please try again later.",); - // req.redirect(req.get("referer")); - // } else { - req.flash( - "mailsendsuccess", - `Verification email sent! The link expires in 1 hour. Please check your email. Also, make sure to check your spam folder. Verification URL: ${verificationUrl}` - ); - res.redirect(req.get("referer")); - // } - // }); + if (user.email == newEmail) { + req.flash( + "mailsenderror", + "Error sending verification email. Provided email is already in use." + ); + res.redirect(req.get("referer")); + } else { + const transporter = nodemailer.createTransport({ + service: "gmail", + auth: { + user: process.env.EMAIL_USER, + pass: process.env.EMAIL_PASSWORD, + }, + }); + // // Generate a verification URL and send it to the user + const verificationUrl = `${req.get( + "origin" + )}/verifyemail?token=${jwt.sign( + { + username: user.username, + email: newEmail, + }, + process.env.AUTHORIZATION_STRING, + { + expiresIn: "1h", + } + )}`; + const mailOptions = { + from: process.env.EMAIL_USER, + to: newEmail, + priority: "high", + subject: + "[That Computer Scientist] Request to change your email address", + html: `

Hi ${user.firstname || user.username},

+

We received a request to change your email address to ${newEmail}.

+

If you made this request, please click the link below to verify your new email address:

+

${verificationUrl}.

+

Please note that this link expires in 1 hour. You might need to make another request if you do not verify the email in the requested time frame. If you did not make this request, you can ignore this email.

+
+

Thanks,

+

Kumar Priyansh

+

That Computer Scientist

`, + }; + transporter.sendMail(mailOptions, (err, info) => { + if (err) { + req.flash( + "mailsenderror", + "Error sending verification email. Please try again later." + ); + res.redirect(req.get("referer")); + } else { + req.flash( + "mailsendsuccess", + `Verification email sent! The link expires in 1 hour. Please check your email. Also, make sure to check your spam folder.` + ); + res.redirect(req.get("referer")); + } + }); + } } else { res.status(500).render("error", { error: "User not found", diff --git a/views/account.ejs b/views/account.ejs index 06e4ca9e..a07d2f3d 100644 --- a/views/account.ejs +++ b/views/account.ejs @@ -89,9 +89,6 @@ <% } %> <% if (locals.messages.mailsendsuccess) { %>

<%= messages.mailsendsuccess %>

- <% } %> -- cgit v1.2.3