From 2650833a9b474bde47575ad2a4ddd57cba186a9f Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 30 May 2020 18:47:39 +0200 Subject: [PATCH] Add list, set service and fix user list for service page --- models/user.js | 7 ++- public/javascripts/service.js | 84 ++++++++++++++++++++++++++--------- sockets/addUser.js | 14 +++--- sockets/index.js | 6 ++- sockets/listService.js | 24 ++++++++++ sockets/setService.js | 20 +++++++++ views/commands.pug | 4 +- views/service.pug | 4 +- 8 files changed, 128 insertions(+), 35 deletions(-) create mode 100644 sockets/listService.js create mode 100644 sockets/setService.js diff --git a/models/user.js b/models/user.js index 39fdcd1..eaa711a 100644 --- a/models/user.js +++ b/models/user.js @@ -1,4 +1,5 @@ "use strict"; + module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { username: { @@ -6,7 +7,11 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true }, passwordHash: { - type: DataTypes.STRING + type: DataTypes.STRING, + set(value) { + if (value) + this.setDataValue("passwordHash", require("crypto").createHash("sha256").update(this.username + value).digest("utf-8")); + } }, firstName: { type: DataTypes.STRING, diff --git a/public/javascripts/service.js b/public/javascripts/service.js index 940282e..d565644 100644 --- a/public/javascripts/service.js +++ b/public/javascripts/service.js @@ -1,32 +1,40 @@ let socket = io(); +let users = {}; +let usersAdd = []; -document.querySelectorAll("input[type='text']").forEach(e => { - e.addEventListener("keyup", ev => {hinter(ev)}) -}); +function addUser(username) { + let firstName, lastName; + do { + firstName = prompt("First name for " + username); + } while (firstName === ""); + if (firstName) { + do { + lastName = prompt("Last name for " + username); + } while (lastName === ""); + if (lastName) + socket.emit("add user", {username: username, firstName: firstName, lastName: lastName}); + } + if (!firstName|| !lastName) + alert("User creation aborted for " + username); +} -document.querySelector("button").addEventListener("click", ev => { - let users= {}; - document.querySelectorAll("input[type='text']").forEach(e=> { - if (e.style.color) - socket.emit("add user", {"username": e.value, "firstname": prompt(`Prénom pour ${e.value}`), "lastname": prompt(`Nom pour ${e.value}`), "password": prompt(`MDP pour ${e.value}`)}); - users[e.id] = e.value; - }); - socket.emit("set service", users); - window.close(); -}); +function next() { + if (usersAdd.length) + addUser(usersAdd.pop()); + else + socket.emit("set service", users); +} function hinter(ev) { let input = ev.target; let min_characters = 0; if (input.value.length < min_characters) return; - socket.emit("list users"); + socket.emit("list user", input.value); } -socket.on("connect", data => { - if (data === "ok") { - socket.emit("list service"); - } +socket.on("connected", () => { + socket.emit("list service"); }); socket.on("list service", data => { @@ -35,18 +43,50 @@ socket.on("list service", data => { } }); -socket.on("list users", data => { +socket.on("list user", data => { let user_list = document.getElementById("user_list"); user_list.innerHTML = ""; - for (let u of data["list"]) { + for (let u of data) { user_list.insertAdjacentHTML("beforeend", `