From 7981f1e04589ae6cf447f93aef33e7bdb91b2d8a Mon Sep 17 00:00:00 2001 From: C4illin Date: Mon, 18 Mar 2024 22:14:56 +0100 Subject: [PATCH] resend email if account isn't active --- backend/index.js | 78 +++++++++++++++++++++++++++++++++++++----------- frontend/app.js | 20 ++++++------- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/backend/index.js b/backend/index.js index badde4e..c790ae5 100644 --- a/backend/index.js +++ b/backend/index.js @@ -96,11 +96,50 @@ app.post("/register", async (req, res) => { let sql = "SELECT * FROM users WHERE email = ?"; let rows = await conn.query(sql, [userEmail]); if (rows.length > 0) { - res - .status(400) - .send( - "Email already in use, search your inbox for a confirmation email or contact an admin for assistance", - ); + // If the account is not active, resend the confirmation email + if (!rows[0].active) { + sql = "SELECT token FROM confirmations WHERE user_id = ?"; + const tokenRows = await conn.query(sql, [rows[0].id]); + if (tokenRows.length > 0) { + const token = tokenRows[0].token; + try { + await sendConfirmationEmail( + userEmail, + req.headers.host, + token, + transporter, + ); + res.send( + "Account already exists but is not active. Confirmation email has been resent. Check your inbox and spam folder.", + ); + } catch (err) { + console.error(err); + res.status(500).send("Failed to send confirmation email"); + return; + } + } else { + res + .status(400) + .send( + "Account already exists but is not active, and no confirmation token found. Contact an admin for assistance.", + ); + } + } else { + res.status(400).send("Account already exists and is active."); + } + return; + } + + try { + await sendConfirmationEmail( + userEmail, + req.headers.host, + token, + transporter, + ); + } catch (err) { + console.error(err); + res.status(500).send("Failed to send confirmation email"); return; } @@ -118,27 +157,32 @@ app.post("/register", async (req, res) => { if (conn) conn.end(); } - // Send email with the token + res.send("Check your email for a confirmation link"); +}); + +function sendConfirmationEmail(userEmail, host, token, transporter) { const mailOptions = { from: `"${process.env.MAIL_NAME}" <${process.env.MAIL_FROM}>`, to: userEmail, subject: "Registration Confirmation for mc.chs.se", text: `Hello, -Please confirm your registration by clicking the following link: http://${req.headers.host}/confirm/${token} +Please confirm your registration by clicking the following link: http://${host}/confirm/${token} If you did not request this, please ignore this email.`, }; - transporter.sendMail(mailOptions, (err) => { - if (err) { - console.error("There was an error: ", err); - res.status(500).send("Failed to send email"); - } else { - console.log("Email sent"); - res.send("Check your email for a confirmation link"); - } + return new Promise((resolve, reject) => { + transporter.sendMail(mailOptions, (err, info) => { + if (err) { + console.error("There was an error: ", err); + reject(err); + } else { + console.log("Email sent: ", info.response); + resolve(info); + } + }); }); -}); +} app.get("/confirm/:token", async (req, res) => { const token = req.params.token; @@ -160,7 +204,7 @@ app.get("/confirm/:token", async (req, res) => { }, 3000); `); } else { - res.send("Invalid token. Try again. Go to back"); + res.send("Invalid token. Try again. Go to start"); } } catch (err) { console.error(err); diff --git a/frontend/app.js b/frontend/app.js index eeeb0e7..bb00a35 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -16,22 +16,22 @@ const converter = new Converter({ newline: true }); tippy("[data-tippy-content]"); -let navbarMenu = document.getElementById("navbarMenu"); +const navbarMenu = document.getElementById("navbarMenu"); -let navbarBurger = document.getElementById("navbar-burger"); -navbarBurger.addEventListener("click", function () { +const navbarBurger = document.getElementById("navbar-burger"); +navbarBurger.addEventListener("click", () => { navbarBurger.classList.toggle("is-active"); navbarMenu.classList.toggle("is-active"); }); -document.body.addEventListener("click", function (e) { +document.body.addEventListener("click", (e) => { if (e.target.classList.contains("chs-modal-close")) { - let modal = e.target.getAttribute("data-modal"); + const modal = e.target.getAttribute("data-modal"); document.getElementById(modal).classList.remove("is-active"); } if (e.target.classList.contains("chs-modal-open")) { - let modal = e.target.getAttribute("data-modal"); + const modal = e.target.getAttribute("data-modal"); document.getElementById(modal).classList.add("is-active"); } @@ -40,7 +40,7 @@ document.body.addEventListener("click", function (e) { } }); -let statusElem = document.getElementById("status"); +const statusElem = document.getElementById("status"); async function updateMOTD() { try { const response = await fetch("/ping"); @@ -51,9 +51,9 @@ async function updateMOTD() { const rendered = mustache.render( document.getElementById("motd-template-success").innerHTML, { - current: data["players"]["online"], - max: data["players"]["max"], - motd: converter.toHTML(converter.parse(data["description"])), + current: data.players.online, + max: data.players.max, + motd: converter.toHTML(converter.parse(data.description)), } ); statusElem.innerHTML = rendered;