summaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorJulien Dessaux2022-10-30 23:16:47 +0100
committerJulien Dessaux2022-10-30 23:16:47 +0100
commitdc5a002144ff7165e73fb38f4be186d827df98c6 (patch)
treec121abb7e34ae56adb33ca612d5ced962b411b63 /routes
parentAdded a basic database schema and migration function (diff)
downloadjeux-de-mots-dc5a002144ff7165e73fb38f4be186d827df98c6.tar.gz
jeux-de-mots-dc5a002144ff7165e73fb38f4be186d827df98c6.tar.bz2
jeux-de-mots-dc5a002144ff7165e73fb38f4be186d827df98c6.zip
Added register and login database functions, and use the last one in the login route
Diffstat (limited to 'routes')
-rw-r--r--routes/login.js16
-rw-r--r--routes/root.js2
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");