From 0116c966d3d6ea914ec4b9e3e5ec6e405d37f22d Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Mon, 29 Aug 2022 10:45:26 +0200 Subject: login --- server.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'server.js') diff --git a/server.js b/server.js index 791a227..e9d0761 100755 --- a/server.js +++ b/server.js @@ -145,27 +145,40 @@ io.on("connection", socket => { } console.log(`new socket connection id=${socket_id} (nc=${num_connected})`); + let logged_in = false; + socket.on("disconnect", () => { num_connected--; console.log(`disconnect id=${socket_id} (nc=${num_connected})`); }); + socket.on("login", pass => { + if (pass == fs.readFileSync(__dirname + "/password.txt").toString().trim()) { + console.log(`login id=${socket_id}`); + logged_in = true; + socket.emit("init", database.get()); + } else { + socket.emit("unauth"); + } + }); + socket.on("new", string => { + if (!logged_in) { socket.emit("unauth"); return; } const res = database.add_new(string); if (res != null) socket.emit("error", res); }); socket.on("set_votes", (string, votes) => { + if (!logged_in) { socket.emit("unauth"); return; } const res = database.set_votes(string, votes); if (res != null) socket.emit("error", res); }); socket.on("delete", string =>{ + if (!logged_in) { socket.emit("unauth"); return; } const res = database.remove(string); if (res != null) socket.emit("error", res); }); - - socket.emit("init", database.get()); }); -- cgit v1.2.3-70-g09d2