summaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorJulien Dessaux2022-11-19 18:41:35 +0100
committerJulien Dessaux2022-11-19 18:41:35 +0100
commit54e06aa44a3134dfc42db3e4b473abf5e181fc42 (patch)
tree35c2358e0cbe47d21972535b8485aa0fd9756b8d /routes
parentUpdated dependencies and fixed lint errors (diff)
downloadjeux-de-mots-54e06aa44a3134dfc42db3e4b473abf5e181fc42.tar.gz
jeux-de-mots-54e06aa44a3134dfc42db3e4b473abf5e181fc42.tar.bz2
jeux-de-mots-54e06aa44a3134dfc42db3e4b473abf5e181fc42.zip
cleaned up the distinction between routes and controllers
Diffstat (limited to '')
-rw-r--r--routes/login.js48
-rw-r--r--routes/logout.js16
-rw-r--r--routes/root.js24
3 files changed, 18 insertions, 70 deletions
diff --git a/routes/login.js b/routes/login.js
deleted file mode 100644
index 89299dd..0000000
--- a/routes/login.js
+++ /dev/null
@@ -1,48 +0,0 @@
-import express from "express";
-import { check, validationResult } from "express-validator";
-
-import makeLoginController from "../controllers/login.js";
-import bodyParser from "../middlewares/formParser.js";
-import session from "../middlewares/sessions.js";
-import { login } from "../database/users.js";
-
-const router = express.Router();
-router.use(session);
-
-router.get("/", (req, res) => {
- if (req.session.user !== undefined) {
- return res.redirect(302, "/play");
- }
- return res.render("login", makeLoginController(req));
-});
-
-const checkUsername = check("username")
- .trim()
- .matches(/^[a-z][-a-z0-9_]+$/i)
- .withMessage("Un identifiant d'au moins deux charactères est requis.");
-const checkPassword = check("password")
- .isStrongPassword()
- .withMessage("Veuillez utiliser un mot de passe d'au moins 8 caractères contenant au moins une minuscule, majuscule, chiffre et charactère spécial.");
-
-router.post("/", [bodyParser, checkUsername, checkPassword], async (req, res) => {
- if (req.session.user !== undefined) {
- return res.redirect(302, "/play");
- }
- let controller = makeLoginController(req);
- controller.data = req.body;
- controller.errors = validationResult(req).mapped();
- if (Object.keys(controller.errors).length === 0) {
- const user = await login(controller.data.username, controller.data.password);
- if (user !== null) {
- req.session.user = user;
- } else {
- controller.errors.mismatch = "L'identifiant et le mot de passe ne correspondent pas, ou l'identifiant n'existe pas.";
- }
- }
- if (Object.keys(controller.errors).length === 0) {
- return res.redirect(302, "/play");
- }
- return res.render("login", controller);
-});
-
-export default router;
diff --git a/routes/logout.js b/routes/logout.js
deleted file mode 100644
index a92085a..0000000
--- a/routes/logout.js
+++ /dev/null
@@ -1,16 +0,0 @@
-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) {
- res.clearCookie("JDMSessionId");
- req.session.destroy();
- }
- return res.redirect(302, "/");
-});
-
-export default router;
diff --git a/routes/root.js b/routes/root.js
index d8e6b55..c41f3e1 100644
--- a/routes/root.js
+++ b/routes/root.js
@@ -1,14 +1,26 @@
import express from "express";
+import { check } from "express-validator";
+import { login_get, login_post } from "../controllers/root/login.js";
+import { logout_get } from "../controllers/root/logout.js";
+import { root_get } from "../controllers/root/root.js";
+import bodyParser from "../middlewares/formParser.js";
import session from "../middlewares/sessions.js";
const router = express.Router();
router.use(session);
-router.get("/", (req, res) => {
- if (req.session.user !== undefined) {
- return res.redirect(302, "/play");
- }
- return res.redirect(302, "/login");
-});
+
+const checkUsername = check("username")
+ .trim()
+ .matches(/^[a-z][-a-z0-9_]+$/i)
+ .withMessage("Un identifiant d'au moins deux charactères est requis.");
+const checkPassword = check("password")
+ .isStrongPassword()
+ .withMessage("Veuillez utiliser un mot de passe d'au moins 8 caractères contenant au moins une minuscule, majuscule, chiffre et charactère spécial.");
+
+router.get("/", root_get);
+router.get("/login", login_get);
+router.post("/login", [bodyParser, checkUsername, checkPassword], login_post);
+router.get("/logout", logout_get);
export default router;