diff options
Diffstat (limited to '')
-rw-r--r-- | routes/login.js | 16 | ||||
-rw-r--r-- | routes/root.js | 2 |
2 files changed, 11 insertions, 7 deletions
diff --git a/routes/login.js b/routes/login.js index 9b04324..4bcd42f 100644 --- a/routes/login.js +++ b/routes/login.js @@ -4,12 +4,13 @@ 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"; +import { login } from "../database/users.js"; const router = express.Router(); router.use(session); router.get("/", (req, res) => { - if (req.session.userId) { + if (req.session.user !== undefined) { return res.redirect(302, "/play"); } return res.render("login", makeLoginController()); @@ -23,17 +24,20 @@ 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) { +router.post("/", [bodyParser, checkUsername, checkPassword], async (req, res) => { + if (req.session.user !== undefined) { 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 + 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"); diff --git a/routes/root.js b/routes/root.js index be3681a..d8e6b55 100644 --- a/routes/root.js +++ b/routes/root.js @@ -5,7 +5,7 @@ import session from "../middlewares/sessions.js"; const router = express.Router(); router.use(session); router.get("/", (req, res) => { - if (session.loggedIn) { + if (req.session.user !== undefined) { return res.redirect(302, "/play"); } return res.redirect(302, "/login"); |