summaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorJulien Dessaux2022-10-27 23:56:29 +0200
committerJulien Dessaux2022-10-27 23:56:29 +0200
commit2611ded920a8345922346ed0d43e4efdd94401a1 (patch)
tree3313d976fbb11bdbfd1841ce336cc4372f9ab32c /routes
parentAllow for placing the first word (diff)
downloadjeux-de-mots-2611ded920a8345922346ed0d43e4efdd94401a1.tar.gz
jeux-de-mots-2611ded920a8345922346ed0d43e4efdd94401a1.tar.bz2
jeux-de-mots-2611ded920a8345922346ed0d43e4efdd94401a1.zip
Began implementing middlewares, session and auth
Diffstat (limited to '')
-rw-r--r--routes/login.js44
-rw-r--r--routes/play.js35
-rw-r--r--routes/root.js14
3 files changed, 78 insertions, 15 deletions
diff --git a/routes/login.js b/routes/login.js
new file mode 100644
index 0000000..9b04324
--- /dev/null
+++ b/routes/login.js
@@ -0,0 +1,44 @@
+import express from "express";
+import { check, validationResult, matchedData } from "express-validator";
+
+import makeLoginController from "../controllers/login.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.userId) {
+ return res.redirect(302, "/play");
+ }
+ return res.render("login", makeLoginController());
+});
+
+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], (req, res) => {
+ if (req.session.userId) {
+ return res.redirect(302, "/play");
+ }
+ let controller = makeLoginController();
+ controller.data = req.body;
+ controller.errors = validationResult(req).mapped();
+ if (Object.keys(controller.errors).length === 0) {
+ // TODO check password
+ req.session.userId = 1;
+ // TODO add an error variable for the username/password mismatch
+ }
+ if (Object.keys(controller.errors).length === 0) {
+ return res.redirect(302, "/play");
+ }
+ return res.render("login", controller);
+});
+
+export default router;
diff --git a/routes/play.js b/routes/play.js
index 5e904c4..1c87895 100644
--- a/routes/play.js
+++ b/routes/play.js
@@ -1,25 +1,30 @@
import express from "express";
+import requireAuth from "../middlewares/requireAuth.js";
+import session from "../middlewares/sessions.js";
+
const router = express.Router();
+router.use(session);
+router.use(requireAuth);
router.get("/", (req, res) => {
const CWDATA = {
board: [
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "T", "E", "S","T", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ],
- [ "", "", "", "", "","", "", "", "", "","", "", "", "", "" ]
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ],
+ [ "", "", "", "", "","", "", "", "", "", "", "", "", "", "" ]
],
letters: [ "A", "B", "C", "D", "E", "F", "JOKER" ]
};
diff --git a/routes/root.js b/routes/root.js
new file mode 100644
index 0000000..be3681a
--- /dev/null
+++ b/routes/root.js
@@ -0,0 +1,14 @@
+import express from "express";
+
+import session from "../middlewares/sessions.js";
+
+const router = express.Router();
+router.use(session);
+router.get("/", (req, res) => {
+ if (session.loggedIn) {
+ return res.redirect(302, "/play");
+ }
+ return res.redirect(302, "/login");
+});
+
+export default router;