Skip to content

Commit

Permalink
resend email if account isn't active
Browse files Browse the repository at this point in the history
  • Loading branch information
C4illin committed Mar 18, 2024
1 parent 8be54e6 commit 7981f1e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
78 changes: 61 additions & 17 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -160,7 +204,7 @@ app.get("/confirm/:token", async (req, res) => {
}, 3000);
</script>`);
} else {
res.send("Invalid token. Try again. <a href='/'>Go to back</a>");
res.send("Invalid token. Try again. <a href='/'>Go to start</a>");
}
} catch (err) {
console.error(err);
Expand Down
20 changes: 10 additions & 10 deletions frontend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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");
Expand All @@ -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;
Expand Down

0 comments on commit 7981f1e

Please sign in to comment.