From 6bedf459370fe3de87765aa0c544de796f3340cf Mon Sep 17 00:00:00 2001 From: clement callaert Date: Wed, 1 Nov 2023 03:01:18 +0100 Subject: [PATCH] Version 1 --- routes/auth.js | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ routes/demo.js | 57 +++++++++++++++ routes/posts.js | 40 +++++++++++ 3 files changed, 278 insertions(+) create mode 100644 routes/auth.js create mode 100644 routes/demo.js create mode 100644 routes/posts.js diff --git a/routes/auth.js b/routes/auth.js new file mode 100644 index 0000000..8154103 --- /dev/null +++ b/routes/auth.js @@ -0,0 +1,181 @@ +const express = require('express'); +const mongodb = require('mongodb') +const bcrypt = require('bcryptjs'); + +const db = require('../data/database'); + +const router = express.Router(); + +router.get('/creer-compte', function (req, res) { + let sessionInputData = { + hasError: false, + nom: '', + prenom: '', + nom_dutilisateur: '', + email: '', + confirmEmail: '', + password: '', + confirmPassword: '' + }; + req.session.input = null; + res.render('creer_compte', {inputData: sessionInputData}); +}); + +router.post('/creer-compte', async function (req, res) { + const userData = req.body; + const enteredNom = userData.nom; + const enteredPrenom = userData.prenom; + const enteredNom_dutilisateur = userData.nom_dutilisateur; + const enteredEmail = userData.email; + const enteredConfirmEmail = userData['confirm-email']; + const enteredPassword = userData.password; + const enteredConfirmPassword = userData['confirm-password']; + + if (!enteredNom || + !enteredPrenom || + !enteredNom_dutilisateur || + !enteredEmail || + !enteredConfirmEmail || + !enteredConfirmPassword || + !enteredPassword || + enteredPassword < 6 || + enteredEmail !== enteredConfirmEmail || + enteredPassword !== enteredConfirmPassword || + !enteredEmail.includes('@') + ) { + req.session.inputData = { + hasError: true, + message: 'Invalid input - please check your data.', + nom: enteredNom, + prenom: enteredPrenom, + nom_dutilisateur: enteredNom_dutilisateur, + email: enteredEmail, + confirmEmail: enteredConfirmEmail, + password: enteredPassword, + confirmPassword: enteredConfirmPassword + }; + + req.session.save(function () { + res.redirect('/creer-compte') + }); + return; + } + + const existingUser = await db.getDb().collection('users').findOne({email: enteredEmail}); + + if (existingUser) { + req.session.inputData = { + hasError: true, + message: 'User exists already!', + nom: enteredNom, + prenom: enteredPrenom, + nom_dutilisateur: enteredNom_dutilisateur, + email: enteredEmail, + confirmEmail: enteredConfirmEmail, + password: enteredPassword, + confirmPassword: enteredConfirmPassword + }; + req.session.save(function () { + res.redirect('/creer-compte') + }); + return; + } + + const hashedPassword = await bcrypt.hash(enteredPassword, 12); + + const user = { + nom: enteredNom, + prenom: enteredPrenom, + nom_dutilisateur: enteredNom_dutilisateur, + email: enteredEmail, + password: hashedPassword, + isAdmin: false + } + + await db.getDb().collection('users').insertOne(user); + + return res.redirect('/connexion'); +}); + +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}); +}); + + + +router.post('/connexion', async function (req, res) { + const userData = req.body; + const enteredNom_dutilisateur = userData.nom_dutilisateur; + const enteredPassword = userData.password; + + const existingUser = await db.getDb().collection('users').findOne({nom_dutilisateur: enteredNom_dutilisateur}); + + if (!existingUser) { + 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; + } + + const passwordsAreEqual = await bcrypt.compare(enteredPassword, existingUser.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: existingUser._id, nom_dutilisateur: existingUser.nom_dutilisateur}; + req.session.isAuthenticated = true; + req.session.save(function () { + res.redirect('/'); + }); +}); + +router.post('/supprimer-compte', function (req,res) { + db.getDb().collection('users').deleteOne({_id: req.session.id}) + req.session.user = null; + req.session.isAuthenticated = false; + res.redirect('/'); +}) + + +router.post('/logout', function (req, res) { + req.session.user = null; + req.session.isAuthenticated = false; + res.redirect('/'); +}); + +router.post('mettreAdmin', async function (req,res) { + const enteredNomDutilisateur = res.body.utilisateurs; + + await db.getDb().collection('users').updateOne({nom_dutilisateur: enteredNomDutilisateur}, {$set: {isAdmin: true}}) +}) + +router.post('/supprAdmin', async function (req,res) { + const enteredNomDutilisateur = res.body.utilisateurs; + await db.getDb().collection('users').updateOne({nom_dutilisateur: enteredNomDutilisateur}, {$set: {isAdmin: false}}) +}) + + +module.exports = router \ No newline at end of file diff --git a/routes/demo.js b/routes/demo.js new file mode 100644 index 0000000..55431b0 --- /dev/null +++ b/routes/demo.js @@ -0,0 +1,57 @@ +const express = require('express'); + +const bcrypt = require('bcryptjs'); + +const db = require('../data/database'); +const Post = require('../models/post') + +const router = express.Router(); + +router.get('/', function (req, res) { + res.render('index'); +}); + +router.get('/profile', function(req, res) { + if (!req.session.isAuthenticated) { + return res.status(401).render('401'); + } + res.render('profile'); +}); + +router.get('/admin', async function (req, res) { + if (!req.session.isAuthenticated) { + return res.status(401).render('401'); + } + const user = await db.getDb().collection('users').findOne({_id: req.session.user.id}) + if (!user || !user.isAdmin) { + return res.status(403).render('403'); + } + + const users = await db.getDb().collection('users').find().toArray(); + res.render('adminPage', {users: users}); +}) + + +router.get('/contact', function (req, res) { + res.render('contact') +}) + +router.get('/sports', function (req,res) { + res.render('sports') +}) + +router.get('/services', function (req,res) { + res.render('services') +}) + +router.get('/team', function (req,res) { + res.render('team') +}) + +router.get('/a-propos', function (req,res) { + res.render('a_propos') +}) + + + +module.exports = router; diff --git a/routes/posts.js b/routes/posts.js new file mode 100644 index 0000000..cfc6e89 --- /dev/null +++ b/routes/posts.js @@ -0,0 +1,40 @@ +const express = require('express'); +const mongodb = require('mongodb') +const bcrypt = require('bcryptjs'); + +const db = require('../data/database'); + +const router = express.Router(); + +router.post('/creerPost', async function (req,res) { + const postData = req.body; + const enteredTitre = postData.titre; + const enteredCommentaire = postData.commentairePost; + const post ={ + titre: enteredTitre, + commentaire: enteredCommentaire, + isFinish: false, + } + await db.getDb().collection('posts').insertOne(post); +}) + +router.get('/hotLine', function (req,res) { + const postData = db.getDb().collection('posts').find().array(); + res.render('hotLine', {postData: postData}) +}) + +router.post('/commandeCrepe', async function (req, res) { + const crepeData = req.body; + const enteredCommentaire = crepeData.commentaire; + const enteredGarniture = crepeData.garniture; + + const crepeCommande = { + garniture: enteredGarniture, + commentaire: enteredCommentaire, + finish: false + } + await db.getDb().collection('commande').insertOne(crepeCommande); + res.redirect("/hotLine"); +}) + +module.exports = router; \ No newline at end of file