From 9d8897fcccfed2fc8a33a5e96e093b63740dee5d Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 12 Oct 2020 21:28:44 +0200 Subject: [PATCH 1/2] Set user model --- models/user.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 models/user.js diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..e6b0f11 --- /dev/null +++ b/models/user.js @@ -0,0 +1,72 @@ +"use strict"; + +const crypto = require("crypto"); +const privateKey = require("../config/config.json").passwordPrivateKey; + +const { + Model +} = require("sequelize"); +module.exports = (sequelize, DataTypes) => { + function hash(password, email) { + let cipher = crypto.createCipheriv( + "aes-256-cbc", + privateKey, + crypto.createHash("md5").update(email).digest("base64").slice(0, 16) + ); + return cipher.update(password, "utf8", "base64") + cipher.final("base64"); + } + + class User extends Model { + static associate(models) { + } + + checkPassword(password) { + return hash(password, this.email) === this.passwordHash + } + } + User.init({ + email: { + type: DataTypes.STRING, + validate: { + isEmail: true + }, + primaryKey: true + }, + emailVerified : { + type: DataTypes.BOOLEAN, + defaultValue: false, + allowNull: false + }, + emailToken: { + type: DataTypes.STRING, + unique: true + }, + firstName: { + type: DataTypes.STRING, + allowNull: false, + unique: "userFullName" + }, + lastName: { + type: DataTypes.STRING, + allowNull: false, + unique: "userFullName" + }, + passwordHash: { + type: DataTypes.STRING, + allowNull: false, + set(value) { + if (value) + this.setDataValue("passwordHash", hash(value, this.email)); + } + }, + permissions: { + type: DataTypes.INTEGER, + defaultValue: 0, + allowNull: false + } + }, { + sequelize, + modelName: "User", + }); + return User; +}; From 6e57b478e411ddc3e37ceeee5658ef411b877718 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 12 Oct 2020 21:29:18 +0200 Subject: [PATCH 2/2] Add login methods and route --- app.js | 2 ++ config/config_example.json | 3 ++- routes/login.js | 8 ++++++ sockets/index.js | 1 + sockets/login.js | 20 +++++++++++++++ views/index.pug | 51 ++----------------------------------- views/login.pug | 52 ++++++++++++++++++++++++++++++++++++++ views/signinup.pug | 3 --- 8 files changed, 87 insertions(+), 53 deletions(-) create mode 100644 routes/login.js create mode 100644 sockets/login.js create mode 100644 views/login.pug delete mode 100644 views/signinup.pug diff --git a/app.js b/app.js index 5375955..07a9422 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ const session = require("express-session"); const config = require("./config/config.json"); let indexRouter = require("./routes/index"); +const loginRouter = require("./routes/login"); let app = express(); const sessionMiddleware = session({secret: config.secret}); @@ -35,6 +36,7 @@ app.use((req, res, next) => { }); app.use("/", indexRouter); +app.use("/login", loginRouter); // catch 404 and forward to error handler app.use((req, res, next) => { diff --git a/config/config_example.json b/config/config_example.json index 93a7605..a930124 100644 --- a/config/config_example.json +++ b/config/config_example.json @@ -7,5 +7,6 @@ "dialect": "postgres", "operatorsAliases": false }, - "secret": "keyboard cat" + "secret": "keyboard cat", + "passwordPrivateKey": "ecc635295f200847b79299df48e15759" } diff --git a/routes/login.js b/routes/login.js new file mode 100644 index 0000000..0c34534 --- /dev/null +++ b/routes/login.js @@ -0,0 +1,8 @@ +let express = require("express"); +let router = express.Router(); + +router.get("/", (req, res) => { + res.render("login", { title: "L'ETU" }); +}); + +module.exports = router; diff --git a/sockets/index.js b/sockets/index.js index 6d72c3e..6d85ff4 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -1,4 +1,5 @@ module.exports = socket => { console.log("New connection !"); + socket.on("login", require("./login")(socket)); socket.emit("connected"); } diff --git a/sockets/login.js b/sockets/login.js new file mode 100644 index 0000000..a38c504 --- /dev/null +++ b/sockets/login.js @@ -0,0 +1,20 @@ +const modules = require("../models"); + +module.exports = socket => { + return async (data) => { + let user = await modules.User.findByPk(data.email); + if (!user) { + socket.emit("login", null); + return; + } + + if (!user.checkPassword(data.password)) { + socket.emit("login", null); + return; + } + + socket.request.session.user = user; + socket.request.session.save(); + socket.emit("login", user) + } +} diff --git a/views/index.pug b/views/index.pug index d50a1b7..a2c3217 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,52 +1,5 @@ extends layout block content - div(class='row main-form') - div(class='col s12 m8 offset-m2 signinup z-depth-5' id="signin") - h2 Welcome back - form - div(class="input-field col s12") - i(class="material-icons prefix") mail - input(type="text" id="mail-input" class="autocomplete") - label(for="mail-input") Email - - div(class="input-field col s12") - i(class="material-icons prefix") lock - input(type="password" id="password-input" class="autocomplete") - label(for="password-input") Password - - input(type='submit' value='Login') - - div(class='col s12 m8 offset-m2 signinup z-depth-5' id="signup") - h2 Welcome - form - div(class="input-field col s6") - i(class="material-icons prefix") portrait - input(type="text" id="firstname-input" class="autocomplete") - label(for="firstname-input") First Name - - div(class="input-field col s6") - input(type="text" id="lastname-input" class="autocomplete") - label(for="lastname-input") Last Name - - div(class="input-field col s12") - i(class="material-icons prefix") mail - input(type="text" id="mailreg-input" class="autocomplete") - label(for="mailreg-input") Email - - div(class="input-field col s12") - i(class="material-icons prefix") lock - input(type="password" id="passwordreg-input" class="autocomplete") - label(for="passwordreg-input") Password - - input(type='submit' value='Register') - - div(class='col s12 m8 offset-m2 signinup z-depth-5' id="forgotPsw") - h2 Forgot password - form - div(class="input-field col s12") - i(class="material-icons prefix") mail - input(type="text" id="mailforgot-input" class="autocomplete") - label(for="mailforgot-input") Email - - input(type='submit' value='Send email') \ No newline at end of file + h1= title + p Welcome to #{title} diff --git a/views/login.pug b/views/login.pug new file mode 100644 index 0000000..11b527a --- /dev/null +++ b/views/login.pug @@ -0,0 +1,52 @@ +extends layout + +block content + div(class='row main-form') + div(class='col s12 m8 offset-m2 signinup z-depth-5' id="signin") + h2 Welcome back + form + div(class="input-field col s12") + i(class="material-icons prefix") mail + input(type="text" id="mail-input" class="autocomplete") + label(for="mail-input") Email + + div(class="input-field col s12") + i(class="material-icons prefix") lock + input(type="password" id="password-input" class="autocomplete") + label(for="password-input") Password + + input(type='submit' value='Login') + + div(class='col s12 m8 offset-m2 signinup z-depth-5' id="signup") + h2 Welcome + form + div(class="input-field col s6") + i(class="material-icons prefix") portrait + input(type="text" id="firstname-input" class="autocomplete") + label(for="firstname-input") First Name + + div(class="input-field col s6") + input(type="text" id="lastname-input" class="autocomplete") + label(for="lastname-input") Last Name + + div(class="input-field col s12") + i(class="material-icons prefix") mail + input(type="text" id="mailreg-input" class="autocomplete") + label(for="mailreg-input") Email + + div(class="input-field col s12") + i(class="material-icons prefix") lock + input(type="password" id="passwordreg-input" class="autocomplete") + label(for="passwordreg-input") Password + + input(type='submit' value='Register') + + div(class='col s12 m8 offset-m2 signinup z-depth-5' id="forgotPsw") + h2 Forgot password + form + div(class="input-field col s12") + i(class="material-icons prefix") mail + input(type="text" id="mailforgot-input" class="autocomplete") + label(for="mailforgot-input") Email + + input(type='submit' value='Send email') diff --git a/views/signinup.pug b/views/signinup.pug deleted file mode 100644 index 4d99d2a..0000000 --- a/views/signinup.pug +++ /dev/null @@ -1,3 +0,0 @@ -extends layout - -block content