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