From 50504a53414370efe98ac937666490c648c984f8 Mon Sep 17 00:00:00 2001 From: clement callaert Date: Mon, 6 Nov 2023 01:15:56 +0100 Subject: [PATCH] login with email --- public/css/adminPage.css | 5 ++++ routes/auth.js | 57 ++++++++++++++++++++++------------------ views/adminPage.ejs | 2 +- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/public/css/adminPage.css b/public/css/adminPage.css index 79a03e5..2dde5de 100644 --- a/public/css/adminPage.css +++ b/public/css/adminPage.css @@ -27,4 +27,9 @@ body { #commentairePost { width: 30rem; height: 10rem; +} + +.imagePhoto { + max-width: 300px; + max-height: 400px; } \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js index 7433610..13ff679 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -7,15 +7,7 @@ const db = require('../data/database'); const router = express.Router(); router.get('/creer-compte', function (req, res) { - let sessionInputData = { - nom: '', - prenom: '', - nom_dutilisateur: '', - email: '', - confirmEmail: '', - password: '', - confirmPassword: '' - }; + let sessionInputData = req.session.inputData; req.session.input = null; res.render('creer_compte', {inputData: sessionInputData}); }); @@ -59,10 +51,10 @@ router.post('/creer-compte', async function (req, res) { }); return; } - console.log("here2") - const existingUser = await db.getDb().collection('users').findOne({email: enteredEmail}) - if (existingUser) { - console.log("here4") + const existingUser1 = await db.getDb().collection('users').findOne({email: enteredEmail}) + const existingUser2 = await db.getDb().collection('users').findOne({nom_dutilisateur: enteredNom_dutilisateur}) + + if (existingUser1 || existingUser2) { req.session.inputData = { hasError: true, message: 'User exists already!', @@ -79,7 +71,6 @@ router.post('/creer-compte', async function (req, res) { }); return; } - console.log("here3") const hashedPassword = await bcrypt.hash(enteredPassword, 12); const user = { @@ -98,12 +89,6 @@ router.post('/creer-compte', async function (req, res) { router.get('/connexion', function (req, res) { let sessionInputData = req.session.inputData; - sessionInputData = { - hasError: false, - nom_dutilisateur: '', - password: '', - } - req.session.input = null; res.render('connexion', {inputData: sessionInputData}); }); @@ -115,9 +100,10 @@ router.post('/connexion', async function (req, res) { const enteredNom_dutilisateur = userData.nom_dutilisateur; const enteredPassword = userData.password; - const existingUser = await db.getDb().collection('users').findOne({nom_dutilisateur: enteredNom_dutilisateur}); + const existingUser1 = await db.getDb().collection('users').findOne({nom_dutilisateur: enteredNom_dutilisateur}); + const existingUser2 = await db.getDb().collection('users').findOne({email: enteredNom_dutilisateur}) - if (!existingUser) { + if (!existingUser1 && !existingUser2) { req.session.inputData = { hasError: true, message: 'Could not log you in - please check your credentials!', @@ -129,8 +115,29 @@ router.post('/connexion', async function (req, res) { }) return; } + if (existingUser1) { + const passwordsAreEqual = await bcrypt.compare(enteredPassword, existingUser1.password); + if (!passwordsAreEqual) { + req.session.inputData = { + hasError: true, + message: 'Could not log you in - please check your credentials!', + nom_dutilisateur: enteredNom_dutilisateur, + password: enteredPassword, + }; + req.session.save(function () { + res.redirect('/connexion') + }) + return; + } + req.session.user = { id: existingUser1._id, nom_dutilisateur: existingUser1.nom_dutilisateur, nom: existingUser1.nom, prenom: existingUser1.prenom, email: existingUser1.email}; + req.session.isAuthenticated = true; + req.session.save(function () { + return res.redirect('/'); + }); + return + } - const passwordsAreEqual = await bcrypt.compare(enteredPassword, existingUser.password); + const passwordsAreEqual = await bcrypt.compare(enteredPassword, existingUser2.password); if (!passwordsAreEqual) { req.session.inputData = { @@ -144,10 +151,10 @@ router.post('/connexion', async function (req, res) { }) return; } - req.session.user = { id: existingUser._id, nom_dutilisateur: existingUser.nom_dutilisateur, nom: existingUser.nom, prenom: existingUser.prenom, email: existingUser.email}; + req.session.user = { id: existingUser2._id, nom_dutilisateur: existingUser2.nom_dutilisateur, nom: existingUser2.nom, prenom: existingUser2.prenom, email: existingUser2.email}; req.session.isAuthenticated = true; req.session.save(function () { - res.redirect('/'); + return res.redirect('/'); }); }); diff --git a/views/adminPage.ejs b/views/adminPage.ejs index 74cf270..b8aa497 100644 --- a/views/adminPage.ejs +++ b/views/adminPage.ejs @@ -130,7 +130,7 @@

<%= res.description %>

- image de la resolution + image de la resolution <% } %> <% }) %>