1
0
Fork 0

Fix redirect & Logout function

This commit is contained in:
Roméo PHANG 2020-11-03 12:54:40 +01:00
parent 5f2dff2aec
commit 3edf78d891
13 changed files with 54 additions and 14 deletions

View file

@ -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 = "/";
}
})

View file

@ -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 = "/";
}
})

View file

@ -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 = "/";
}
});
})

View file

@ -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" });
});

View file

@ -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}});
};

View file

@ -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");
}

10
sockets/logout.js Normal file
View file

@ -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"}});
}
}
}

View file

@ -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))

View file

@ -19,4 +19,3 @@ module.exports = socket => {
}
}
}
// Test Push

View file

@ -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 ?

View file

@ -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")

View file

@ -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")

View file

@ -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