login with email
This commit is contained in:
parent
f702e28f76
commit
50504a5341
3 changed files with 38 additions and 26 deletions
|
@ -27,4 +27,9 @@ body {
|
|||
#commentairePost {
|
||||
width: 30rem;
|
||||
height: 10rem;
|
||||
}
|
||||
|
||||
.imagePhoto {
|
||||
max-width: 300px;
|
||||
max-height: 400px;
|
||||
}
|
|
@ -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('/');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -130,7 +130,7 @@
|
|||
<p>
|
||||
<%= res.description %>
|
||||
</p>
|
||||
<img src="/<%= res.imagePath %>" alt="image de la resolution">
|
||||
<img src="/<%= res.imagePath %>" alt="image de la resolution" class="imagePhoto">
|
||||
</div>
|
||||
<% } %>
|
||||
<% }) %>
|
||||
|
|
Loading…
Reference in a new issue