From b91bb0a133aa3d0400beb359fed4e641c888ab26 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 31 Oct 2022 22:40:42 +0100 Subject: Added logout route, and a way to display the logout menu item on relevant pages --- controllers/login.js | 7 ++++--- controllers/play.js | 9 +++++++++ main.js | 2 ++ routes/login.js | 4 ++-- routes/logout.js | 15 +++++++++++++++ routes/play.js | 5 +++-- static/index.css | 8 ++++++++ views/nav.ejs | 3 +++ 8 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 controllers/play.js create mode 100644 routes/logout.js diff --git a/controllers/login.js b/controllers/login.js index bff312e..b274a9e 100644 --- a/controllers/login.js +++ b/controllers/login.js @@ -1,6 +1,7 @@ -function makeLoginController() { +function makeController(req) { return { - title: "Connection", + title: "Connexion", + user: req.session.user, data: { username: "", password: "", @@ -9,4 +10,4 @@ function makeLoginController() { }; } -export default makeLoginController; +export default makeController; diff --git a/controllers/play.js b/controllers/play.js new file mode 100644 index 0000000..c00d15d --- /dev/null +++ b/controllers/play.js @@ -0,0 +1,9 @@ +function makeController(req, cwdata) { + return { + title: "Jouer", + user: req.session.user, + CWDATA: cwdata, + }; +} + +export default makeController; diff --git a/main.js b/main.js index 9ad63c7..f4bd1a0 100644 --- a/main.js +++ b/main.js @@ -2,6 +2,7 @@ import express from "express"; import helmet from "./middlewares/helmet.js"; import loginRouter from "./routes/login.js"; +import logoutRouter from "./routes/logout.js"; import playRouter from "./routes/play.js"; import rootRouter from "./routes/root.js"; @@ -12,6 +13,7 @@ app.set("views", "./views"); app.set("view engine", "ejs"); app.use("/login", loginRouter); +app.use("/logout", logoutRouter); app.use("/play", playRouter); app.use("/static", express.static("static")); app.use("/", rootRouter); diff --git a/routes/login.js b/routes/login.js index 4bcd42f..9684f8a 100644 --- a/routes/login.js +++ b/routes/login.js @@ -13,7 +13,7 @@ router.get("/", (req, res) => { if (req.session.user !== undefined) { return res.redirect(302, "/play"); } - return res.render("login", makeLoginController()); + return res.render("login", makeLoginController(req)); }); const checkUsername = check("username") @@ -28,7 +28,7 @@ router.post("/", [bodyParser, checkUsername, checkPassword], async (req, res) => if (req.session.user !== undefined) { return res.redirect(302, "/play"); } - let controller = makeLoginController(); + let controller = makeLoginController(req); controller.data = req.body; controller.errors = validationResult(req).mapped(); if (Object.keys(controller.errors).length === 0) { diff --git a/routes/logout.js b/routes/logout.js new file mode 100644 index 0000000..a31d9fe --- /dev/null +++ b/routes/logout.js @@ -0,0 +1,15 @@ +import express from "express"; + +import session from "../middlewares/sessions.js"; + +const router = express.Router(); +router.use(session); + +router.get("/", (req, res) => { + if (req.session.user !== undefined) { + req.session.destroy(); + } + return res.redirect(302, "/"); +}); + +export default router; diff --git a/routes/play.js b/routes/play.js index 1c87895..4db9acd 100644 --- a/routes/play.js +++ b/routes/play.js @@ -1,5 +1,6 @@ import express from "express"; +import makePlayController from "../controllers/play.js"; import requireAuth from "../middlewares/requireAuth.js"; import session from "../middlewares/sessions.js"; @@ -8,7 +9,7 @@ router.use(session); router.use(requireAuth); router.get("/", (req, res) => { - const CWDATA = { + const cwdata = { board: [ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ], [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ], @@ -28,7 +29,7 @@ router.get("/", (req, res) => { ], letters: [ "A", "B", "C", "D", "E", "F", "JOKER" ] }; - return res.render("play", {CWDATA: CWDATA, title: "Jouer"}); + return res.render("play", makePlayController(req, cwdata)); }); export default router; diff --git a/static/index.css b/static/index.css index f0ed5e9..f5ef0b9 100644 --- a/static/index.css +++ b/static/index.css @@ -49,12 +49,20 @@ header nav ul li { padding: 14px 16px; text-decoration: none; } +header nav ul li a { + color: #b58900; + display: block; + text-decoration: none; +} header nav ul li.nav-menu-title { float: left; text-transform: uppercase; font-weight: 700; margin-right: 4px; } +header nav ul li.nav-menu-margin-left { + margin-left: 4px; +} form input { margin-bottom: 8px; } diff --git a/views/nav.ejs b/views/nav.ejs index b494b55..8298004 100644 --- a/views/nav.ejs +++ b/views/nav.ejs @@ -2,6 +2,9 @@ -- cgit v1.2.3