Skip to content

Commit 7981f1e

Browse files
committed
resend email if account isn't active
1 parent 8be54e6 commit 7981f1e

File tree

2 files changed

+71
-27
lines changed

2 files changed

+71
-27
lines changed

backend/index.js

+61-17
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,50 @@ app.post("/register", async (req, res) => {
9696
let sql = "SELECT * FROM users WHERE email = ?";
9797
let rows = await conn.query(sql, [userEmail]);
9898
if (rows.length > 0) {
99-
res
100-
.status(400)
101-
.send(
102-
"Email already in use, search your inbox for a confirmation email or contact an admin for assistance",
103-
);
99+
// If the account is not active, resend the confirmation email
100+
if (!rows[0].active) {
101+
sql = "SELECT token FROM confirmations WHERE user_id = ?";
102+
const tokenRows = await conn.query(sql, [rows[0].id]);
103+
if (tokenRows.length > 0) {
104+
const token = tokenRows[0].token;
105+
try {
106+
await sendConfirmationEmail(
107+
userEmail,
108+
req.headers.host,
109+
token,
110+
transporter,
111+
);
112+
res.send(
113+
"Account already exists but is not active. Confirmation email has been resent. Check your inbox and spam folder.",
114+
);
115+
} catch (err) {
116+
console.error(err);
117+
res.status(500).send("Failed to send confirmation email");
118+
return;
119+
}
120+
} else {
121+
res
122+
.status(400)
123+
.send(
124+
"Account already exists but is not active, and no confirmation token found. Contact an admin for assistance.",
125+
);
126+
}
127+
} else {
128+
res.status(400).send("Account already exists and is active.");
129+
}
130+
return;
131+
}
132+
133+
try {
134+
await sendConfirmationEmail(
135+
userEmail,
136+
req.headers.host,
137+
token,
138+
transporter,
139+
);
140+
} catch (err) {
141+
console.error(err);
142+
res.status(500).send("Failed to send confirmation email");
104143
return;
105144
}
106145

@@ -118,27 +157,32 @@ app.post("/register", async (req, res) => {
118157
if (conn) conn.end();
119158
}
120159

121-
// Send email with the token
160+
res.send("Check your email for a confirmation link");
161+
});
162+
163+
function sendConfirmationEmail(userEmail, host, token, transporter) {
122164
const mailOptions = {
123165
from: `"${process.env.MAIL_NAME}" <${process.env.MAIL_FROM}>`,
124166
to: userEmail,
125167
subject: "Registration Confirmation for mc.chs.se",
126168
text: `Hello,
127-
Please confirm your registration by clicking the following link: http://${req.headers.host}/confirm/${token}
169+
Please confirm your registration by clicking the following link: http://${host}/confirm/${token}
128170
129171
If you did not request this, please ignore this email.`,
130172
};
131173

132-
transporter.sendMail(mailOptions, (err) => {
133-
if (err) {
134-
console.error("There was an error: ", err);
135-
res.status(500).send("Failed to send email");
136-
} else {
137-
console.log("Email sent");
138-
res.send("Check your email for a confirmation link");
139-
}
174+
return new Promise((resolve, reject) => {
175+
transporter.sendMail(mailOptions, (err, info) => {
176+
if (err) {
177+
console.error("There was an error: ", err);
178+
reject(err);
179+
} else {
180+
console.log("Email sent: ", info.response);
181+
resolve(info);
182+
}
183+
});
140184
});
141-
});
185+
}
142186

143187
app.get("/confirm/:token", async (req, res) => {
144188
const token = req.params.token;
@@ -160,7 +204,7 @@ app.get("/confirm/:token", async (req, res) => {
160204
}, 3000);
161205
</script>`);
162206
} else {
163-
res.send("Invalid token. Try again. <a href='/'>Go to back</a>");
207+
res.send("Invalid token. Try again. <a href='/'>Go to start</a>");
164208
}
165209
} catch (err) {
166210
console.error(err);

frontend/app.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ const converter = new Converter({ newline: true });
1616

1717
tippy("[data-tippy-content]");
1818

19-
let navbarMenu = document.getElementById("navbarMenu");
19+
const navbarMenu = document.getElementById("navbarMenu");
2020

21-
let navbarBurger = document.getElementById("navbar-burger");
22-
navbarBurger.addEventListener("click", function () {
21+
const navbarBurger = document.getElementById("navbar-burger");
22+
navbarBurger.addEventListener("click", () => {
2323
navbarBurger.classList.toggle("is-active");
2424
navbarMenu.classList.toggle("is-active");
2525
});
2626

27-
document.body.addEventListener("click", function (e) {
27+
document.body.addEventListener("click", (e) => {
2828
if (e.target.classList.contains("chs-modal-close")) {
29-
let modal = e.target.getAttribute("data-modal");
29+
const modal = e.target.getAttribute("data-modal");
3030
document.getElementById(modal).classList.remove("is-active");
3131
}
3232

3333
if (e.target.classList.contains("chs-modal-open")) {
34-
let modal = e.target.getAttribute("data-modal");
34+
const modal = e.target.getAttribute("data-modal");
3535
document.getElementById(modal).classList.add("is-active");
3636
}
3737

@@ -40,7 +40,7 @@ document.body.addEventListener("click", function (e) {
4040
}
4141
});
4242

43-
let statusElem = document.getElementById("status");
43+
const statusElem = document.getElementById("status");
4444
async function updateMOTD() {
4545
try {
4646
const response = await fetch("/ping");
@@ -51,9 +51,9 @@ async function updateMOTD() {
5151
const rendered = mustache.render(
5252
document.getElementById("motd-template-success").innerHTML,
5353
{
54-
current: data["players"]["online"],
55-
max: data["players"]["max"],
56-
motd: converter.toHTML(converter.parse(data["description"])),
54+
current: data.players.online,
55+
max: data.players.max,
56+
motd: converter.toHTML(converter.parse(data.description)),
5757
}
5858
);
5959
statusElem.innerHTML = rendered;

0 commit comments

Comments
 (0)