summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--controllers/login.js13
-rw-r--r--controllers/root/login.js43
-rw-r--r--controllers/root/logout.js7
-rw-r--r--controllers/root/root.js6
-rw-r--r--main.js4
-rw-r--r--routes/login.js48
-rw-r--r--routes/logout.js16
-rw-r--r--routes/root.js24
8 files changed, 74 insertions, 87 deletions
diff --git a/controllers/login.js b/controllers/login.js
deleted file mode 100644
index b274a9e..0000000
--- a/controllers/login.js
+++ /dev/null
@@ -1,13 +0,0 @@
-function makeController(req) {
- return {
- title: "Connexion",
- user: req.session.user,
- data: {
- username: "",
- password: "",
- },
- errors: {},
- };
-}
-
-export default makeController;
diff --git a/controllers/root/login.js b/controllers/root/login.js
new file mode 100644
index 0000000..b3e407b
--- /dev/null
+++ b/controllers/root/login.js
@@ -0,0 +1,43 @@
+import { validationResult } from "express-validator";
+
+import { login } from "../../database/users.js";
+
+function makePageData(user) {
+ return {
+ title: "Connexion",
+ user: user,
+ data: {
+ username: "",
+ password: "",
+ },
+ errors: {},
+ };
+}
+
+export function login_get(req, res) {
+ if (req.session.user !== undefined) {
+ return res.redirect(302, "/play");
+ }
+ return res.render("login", makePageData(req.session.user));
+}
+
+export async function login_post(req, res) {
+ if (req.session.user !== undefined) {
+ return res.redirect(302, "/play");
+ }
+ let page = makePageData(req.session.user);
+ page.data = req.body;
+ page.errors = validationResult(req).mapped();
+ if (Object.keys(page.errors).length === 0) {
+ const user = await login(page.data.username, page.data.password);
+ if (user !== null) {
+ req.session.user = user;
+ } else {
+ page.errors.mismatch = "L'identifiant et le mot de passe ne correspondent pas, ou l'identifiant n'existe pas.";
+ }
+ }
+ if (Object.keys(page.errors).length === 0) {
+ return res.redirect(302, "/games");
+ }
+ return res.render("login", page);
+}
diff --git a/controllers/root/logout.js b/controllers/root/logout.js
new file mode 100644
index 0000000..078df9a
--- /dev/null
+++ b/controllers/root/logout.js
@@ -0,0 +1,7 @@
+export function logout_get(req, res) {
+ if (req.session.user !== undefined) {
+ res.clearCookie("JDMSessionId");
+ req.session.destroy();
+ }
+ return res.redirect(302, "/");
+}
diff --git a/controllers/root/root.js b/controllers/root/root.js
new file mode 100644
index 0000000..062edf4
--- /dev/null
+++ b/controllers/root/root.js
@@ -0,0 +1,6 @@
+export function root_get(req, res) {
+ if (req.session.user !== undefined) {
+ return res.redirect(302, "/games");
+ }
+ return res.redirect(302, "/login");
+}
diff --git a/main.js b/main.js
index 63528cc..b4c68b2 100644
--- a/main.js
+++ b/main.js
@@ -1,8 +1,6 @@
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";
@@ -13,8 +11,6 @@ app.use(helmet);
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
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;