From 3edf78d8918bb1567def4a1ab2e4bdf3c6bb42e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rom=C3=A9o=20PHANG?= Date: Tue, 3 Nov 2020 12:54:40 +0100 Subject: [PATCH] Fix redirect & Logout function --- public/javascripts/login.js | 6 +++++- public/javascripts/main.js | 21 +++++++++++++++++++++ public/javascripts/register.js | 8 +++++--- routes/register.js | 3 ++- routes/utils/error.js | 2 +- sockets/index.js | 2 ++ sockets/logout.js | 10 ++++++++++ sockets/profile/edit.js | 2 +- sockets/register.js | 3 +-- views/pages/login.pug | 2 +- views/pages/register.pug | 2 +- views/template/layout.pug | 1 + views/template/navbar.pug | 6 +++--- 13 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 public/javascripts/main.js create mode 100644 sockets/logout.js diff --git a/public/javascripts/login.js b/public/javascripts/login.js index a902af0..ece14e1 100644 --- a/public/javascripts/login.js +++ b/public/javascripts/login.js @@ -7,12 +7,16 @@ document.getElementById("login").addEventListener("submit", e=>{ let email = document.getElementById("mail-input").value; let passw = document.getElementById("password-input").value; socket.emit("login", {"email":email,"password":passw}); + }else{ + alert('Format d\'email incorrect.'); } return false; }) socket.on("login", data=>{ - if(data){ + if(data.error){ + alert(data.error.message); + }else{ window.location.href = "/"; } }) \ No newline at end of file diff --git a/public/javascripts/main.js b/public/javascripts/main.js new file mode 100644 index 0000000..1a1fcda --- /dev/null +++ b/public/javascripts/main.js @@ -0,0 +1,21 @@ +let socket = io.connect(); +/* + Front-end Event + */ +// Disconnect +document.getElementById("logout").addEventListener("click", function() { + socket.emit("logout") +}); + + +/* + Socket Actions + */ +// Disconnect +socket.on("logout", data=>{ + if(data.error){ + alert(data.error.message); + }else{ + window.location.href = "/"; + } +}) \ No newline at end of file diff --git a/public/javascripts/register.js b/public/javascripts/register.js index 42a24b5..3b5833a 100644 --- a/public/javascripts/register.js +++ b/public/javascripts/register.js @@ -1,4 +1,4 @@ -//let socket = io.connect(); +let socket = io.connect(); document.getElementById("register").addEventListener("submit", e=>{ e.preventDefault(); @@ -15,7 +15,9 @@ document.getElementById("register").addEventListener("submit", e=>{ }); socket.on("register", data=>{ - if(data){ + if(data.error){ + alert(data.error.message); + }else{ window.location.href = "/"; } -}); +}) diff --git a/routes/register.js b/routes/register.js index 630c8e0..9e99f1c 100644 --- a/routes/register.js +++ b/routes/register.js @@ -1,7 +1,8 @@ let express = require("express"); let router = express.Router(); +const sessionCheck = require("./utils/sessionCheck"); -router.get("/", (req, res) => { +router.get("/",sessionCheck(-1), (req, res) => { res.render("pages/register", { title: "L'ETU" }); }); diff --git a/routes/utils/error.js b/routes/utils/error.js index 81799dc..1bce709 100644 --- a/routes/utils/error.js +++ b/routes/utils/error.js @@ -1,4 +1,4 @@ module.exports = (req, res, message, status, subMessage) => { res.status(status || 500); - res.render("error", {message: message, error: {status: subMessage || undefined}}); + res.render("template/error", {message: message, error: {status: subMessage || undefined}}); }; diff --git a/sockets/index.js b/sockets/index.js index ff1a0c6..604338a 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -1,5 +1,6 @@ module.exports = socket => { console.log("New connection !"); + console.log(socket.request.session.user); if (!socket.request.session.user) { socket.on("login", require("./login")(socket)); socket.on("register", require("./register")(socket)); @@ -8,6 +9,7 @@ module.exports = socket => { socket.on("setPassword", require("./email/setPassword")(socket)); } else { socket.on("profileEdit", require("./profile/edit")(socket)); + socket.on("logout", require("./logout")(socket)); } socket.emit("connected"); } diff --git a/sockets/logout.js b/sockets/logout.js new file mode 100644 index 0000000..2244722 --- /dev/null +++ b/sockets/logout.js @@ -0,0 +1,10 @@ +module.exports = socket => { + return async (data) => { + if(socket.request.session.user){ + socket.request.session.destroy(); + socket.emit("logout", true); + }else{ + socket.emit("logout", {error: { message: "not_logged_in"}}); + } + } +} \ No newline at end of file diff --git a/sockets/profile/edit.js b/sockets/profile/edit.js index 36d3f2f..2fcaff5 100644 --- a/sockets/profile/edit.js +++ b/sockets/profile/edit.js @@ -2,7 +2,7 @@ const models = require("../../models"); module.exports = socket => { return async (data) => { - let user = await models.User.findByPk(data.email); + let user = await models.User.findByPk(socket.request.session.user.email); if (!user) socket.emit("profileEdit", {error: {message: "not_found"}}); else if (!user.checkPassword(data.oldPassword)) diff --git a/sockets/register.js b/sockets/register.js index dc56282..740b2c8 100644 --- a/sockets/register.js +++ b/sockets/register.js @@ -18,5 +18,4 @@ module.exports = socket => { await emailCheck(socket, user, null); } } -} -// Test Push \ No newline at end of file +} \ No newline at end of file diff --git a/views/pages/login.pug b/views/pages/login.pug index aff1956..11a7e62 100644 --- a/views/pages/login.pug +++ b/views/pages/login.pug @@ -22,7 +22,7 @@ html label(for="password-input") Password input(type='submit' value='Login') - button(class="notregister") Not registered ? + a(class="notregister" href="register") Not registered ? diff --git a/views/pages/register.pug b/views/pages/register.pug index f2c0d6f..3ec7d41 100644 --- a/views/pages/register.pug +++ b/views/pages/register.pug @@ -31,7 +31,7 @@ html label(for="passwordreg-input") Password input(type='submit' value='Register') - button(class="alreadyregister") Already registered ? + a(class="alreadyregister" href="login") Already registered ? script(src="/javascripts/register.js") script(src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js") \ No newline at end of file diff --git a/views/template/layout.pug b/views/template/layout.pug index 8fc7fab..165f7ee 100644 --- a/views/template/layout.pug +++ b/views/template/layout.pug @@ -15,4 +15,5 @@ html block navbar div(class="col s10" id="main") block content + script(src="/javascripts/main.js") script(src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js") diff --git a/views/template/navbar.pug b/views/template/navbar.pug index 645db03..00a1580 100644 --- a/views/template/navbar.pug +++ b/views/template/navbar.pug @@ -5,15 +5,15 @@ block navbar div(id="navprofile" class="student") p Kezel Benoit p G4S3 - a Logout + a(id="logout") Logout if teacher === true div(id="navprofile" class="teacher") p Kezel Benoit - a Logout + a(id="logout") Logout if admin === true div(id="navprofile" class="admin") p Kezel Benoit - a Logout + a(id="logout") Logout div(id="navtop") ul