-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseconnecter.js
89 lines (77 loc) · 2.7 KB
/
seconnecter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config()
}
const express = require('express')
const app = express()
const passport = require('passport')
const flash = require('express-flash')
const session = require('express-session')
var bodyParser = require('body-parser')
const methodOverride = require('method-override')
const { Router } = require('express');
const users = require('./users.json')
const router = Router();
const initializePassport = require('./mdpconfig')
const reinitialiser = require("./reinitialisermdp")
const code = reinitialiser.code
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.set('view-engine', 'ejs')
app.disable('view cache');
router.use(express.urlencoded({ extended: false }))
router.use(flash())
router.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}))
router.use(passport.initialize())
router.use(passport.session())
router.use(methodOverride('_method'))
router.get('/connecter', checkNotAuthenticated, (req, res) => {
initializePassport(
passport,
email => users.find(user => user.email === email),
id => users.find(user => user.id === id), code)
res.render('Seconnecter.ejs')
})
/// Fonction qui relie le browzer avec la page HTML ///
router.post('/connecter', checkNotAuthenticated, urlencodedParser, passport.authenticate('local', {
successRedirect: '/profil',
failureRedirect: '/connecter',
failureFlash: true
}))
router.get('/profil', checkAuthenticated, (req, res) => { //dans ce cas pour pouvoir acceder au compte il faut l'identification ( sinon on bloque le chemain )
console.log(req.user.profil)
if ((req.user.profil) === 'Admin') {
return res.render('MenuAdmin.ejs', { name: req.user.nom, firstname: req.user.prenom })
} else if ((req.user.profil) === 'Valideur') {
return res.render('menuValideur.ejs', { name: req.user.nom, firstname: req.user.prenom })
} else {
return res.render('MenuEditeur.ejs', { name: req.user.nom, firstname: req.user.prenom });
}
})
router.delete('/logout', (req, res) => {
req.logOut();
if (req.session) {
req.session.destroy(function(err) {
if (err) {
console.log(err)
}
console.log("Destroyed the user session on Auth0 endpoint");
res.redirect('http://localhost:3000/connecter');
});
}
})
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next()
}
res.redirect('/connecter')
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect('/profil')
}
next()
}
module.exports = router