From b73136b6acf0df3571ff05eced6939c6ff72b9c3 Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 12 Oct 2020 23:24:59 +0200 Subject: [PATCH 1/2] Setup user register --- models/user.js | 6 ++---- public/javascripts/register.js | 21 +++++++++++++++++++++ sockets/index.js | 1 + sockets/register.js | 21 +++++++++++++++++++++ views/login.pug | 3 ++- 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 public/javascripts/register.js create mode 100644 sockets/register.js diff --git a/models/user.js b/models/user.js index 21212fc..d84a83c 100644 --- a/models/user.js +++ b/models/user.js @@ -43,13 +43,11 @@ module.exports = (sequelize, DataTypes) => { }, firstName: { type: DataTypes.STRING, - allowNull: false, - unique: "userFullName" + allowNull: false }, lastName: { type: DataTypes.STRING, - allowNull: false, - unique: "userFullName" + allowNull: false }, passwordHash: { type: DataTypes.STRING, diff --git a/public/javascripts/register.js b/public/javascripts/register.js new file mode 100644 index 0000000..42a24b5 --- /dev/null +++ b/public/javascripts/register.js @@ -0,0 +1,21 @@ +//let socket = io.connect(); + +document.getElementById("register").addEventListener("submit", e=>{ + e.preventDefault(); + let expressionReguliere = /^(([^<>()[]\.,;:s@]+(.[^<>()[]\.,;:s@]+)*)|(.+))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/; + let mail = document.getElementById("mailreg-input").value; + if(expressionReguliere.test(mail)) { + socket.emit("register", { + "email": mail, + "firstName": document.getElementById("firstname-input").value, + "lastName": document.getElementById("lastname-input").value, + "password": document.getElementById("passwordreg-input").value + }); + } +}); + +socket.on("register", data=>{ + if(data){ + window.location.href = "/"; + } +}); diff --git a/sockets/index.js b/sockets/index.js index 6d85ff4..c4b3d28 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -1,5 +1,6 @@ module.exports = socket => { console.log("New connection !"); socket.on("login", require("./login")(socket)); + socket.on("register", require("./register")(socket)); socket.emit("connected"); } diff --git a/sockets/register.js b/sockets/register.js new file mode 100644 index 0000000..334d790 --- /dev/null +++ b/sockets/register.js @@ -0,0 +1,21 @@ +const modules = require("../models"); + +module.exports = socket => { + return async (data) => { + if (await modules.User.findByPk(data.email) || !data.email.endsWith("univ-lyon1.fr")) { + socket.emit("register", null); + return; + } + + let user = await modules.User.create({ + email: data.email, + firstName: data.firstName, + lastName: data.lastName, + passwordHash: data.password + }); + + socket.request.session.user = user; + socket.request.session.save(); + socket.emit("register", user); + } +} diff --git a/views/login.pug b/views/login.pug index ff32fa1..2822c9c 100644 --- a/views/login.pug +++ b/views/login.pug @@ -19,7 +19,7 @@ block content div(class='col s12 m8 offset-m2 signinup z-depth-5' id="signup") h2 Welcome - form + form#register div(class="input-field col s6") i(class="material-icons prefix") portrait input(type="text" id="firstname-input" class="autocomplete") @@ -52,3 +52,4 @@ block content input(type='submit' value='Send email') script(src="/javascripts/login.js") + script(src="/javascripts/register.js") From 0ae014549d47a7d556ffc8ddfb42e4c00903f5ed Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 12 Oct 2020 23:34:51 +0200 Subject: [PATCH 2/2] Setup permissions check on index --- app.js | 6 +++--- routes/index.js | 4 ++-- routes/utils/error.js | 4 ++++ routes/utils/sessionCheck.js | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 routes/utils/error.js create mode 100644 routes/utils/sessionCheck.js diff --git a/app.js b/app.js index cfc8175..fd0d56e 100644 --- a/app.js +++ b/app.js @@ -1,10 +1,10 @@ -let createError = require("http-errors"); let express = require("express"); let path = require("path"); let cookieParser = require("cookie-parser"); let logger = require("morgan"); let sassMiddleware = require("node-sass-middleware"); const session = require("express-session"); +const error = require("./routes/utils/error"); let config = process.env.NODE_ENV === "test" ? {} : require("./config/config.json"); let indexRouter = require("./routes/index"); @@ -42,8 +42,8 @@ app.use("/", indexRouter); app.use("/login", loginRouter); // catch 404 and forward to error handler -app.use((req, res, next) => { - next(createError(404)); +app.use((req, res) => { + return error(req, res, "Page not found", 404); }); // error handler diff --git a/routes/index.js b/routes/index.js index 6b2b7df..b8592d3 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,8 +1,8 @@ let express = require("express"); let router = express.Router(); +const sessionCheck = require("./utils/sessionCheck"); -/* GET home page. */ -router.get("/", (req, res) => { +router.get("/", sessionCheck(1), (req, res) => { res.render("index", { title: "L'ETU" }); }); diff --git a/routes/utils/error.js b/routes/utils/error.js new file mode 100644 index 0000000..81799dc --- /dev/null +++ b/routes/utils/error.js @@ -0,0 +1,4 @@ +module.exports = (req, res, message, status, subMessage) => { + res.status(status || 500); + res.render("error", {message: message, error: {status: subMessage || undefined}}); +}; diff --git a/routes/utils/sessionCheck.js b/routes/utils/sessionCheck.js new file mode 100644 index 0000000..a85ad8f --- /dev/null +++ b/routes/utils/sessionCheck.js @@ -0,0 +1,15 @@ +let error = require("./error"); + +function sessionCheck(permission) { + return (req, res, next) => { + if (!req.session.user) { + req.session.lastUrl = req.originalUrl; + req.session.save(() => res.redirect("/login")); + } else if (req.session.user.permissions < permission) + return error(req, res, "Permission denied !", 403); + else + next(); + } +} + +module.exports = sessionCheck;