Skip to content

Commit 7c0c011

Browse files
authored
Captcha checks added
1 parent d6e6783 commit 7c0c011

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

backend/server.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ app.use(express.static("public"));
1414
if (!existsSync("./db.sqlite")) writeFileSync("./db.sqlite", "");
1515
const io = socket(server);
1616
sqlite.open("db.sqlite");
17+
const sessions = require("./SessionIDManager");
18+
const captchas = new Map();
1719

1820
/**
1921
* Displays an error by emitting to websocket on clientside
@@ -31,17 +33,21 @@ function displayError(msg, data, event, status) {
3133
});
3234
}
3335

36+
37+
3438
io.on("connection", data => {
3539
data.on("getCaptcha", () => {
40+
const captcha = sessions.generateSessionID().substr(0, 6);
3641
io.to(data.id).emit("captcha", {
37-
captcha: sessions.generateSessionID().substr(0, 6),
42+
captcha: captcha,
3843
position: {
3944
x: Math.floor(Math.random() * 150) + 25,
4045
y: Math.floor(Math.random() * 65) + 25
4146
}
4247
});
48+
captchas.set(data.id, captcha);
4349
});
44-
50+
4551
data.on("login", res => {
4652
// If username/password is undefined
4753
if (!res.username || !res.password) return io.to(data.id).emit("login", {
@@ -88,6 +94,8 @@ io.on("connection", data => {
8894

8995
if (/[^\w ]+/.test(res.username)) return displayError("Username should only contain A-Za-z_ ", data, "register", 400);
9096

97+
if(res.captcha !== captchas.get(data.id)) return displayError("Captcha is not correct", data, "register", 400);
98+
9199
const hash = bcrypt.hashSync(res.password, 10);
92100

93101
sqlite.prepare("SELECT * FROM accounts WHERE username = ?").then(prepare => {

0 commit comments

Comments
 (0)