-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthentifcation.js
More file actions
48 lines (39 loc) · 1.32 KB
/
Copy pathAuthentifcation.js
File metadata and controls
48 lines (39 loc) · 1.32 KB
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
const cookieParser = require('cookie-parser');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const Database = require('./Database.js');
const hash = require('hash.js')
const COOKIE_SECRET = 'OuYouNaMarre';
passport.use(new LocalStrategy((username, password, cb) => {
password = hash.sha256().update(password).digest('hex');
return Database.getUserByName(username, (res)=>{
if (res === null)
{
return cb(new Error("Identifiants incorrectes !"));
}
else if (password !== res.password)
{
return cb(new Error("Identifiants incorrectes !"));
}
return cb(null , res);
})
}));
passport.serializeUser((user, cb) => {
console.log("SERIALIEZ");
console.log(user);
cb(null, user.username);
});
passport.deserializeUser((username, cb) => {
console.log("DESERIALIZE");
console.log(username);
return Database.getUserByName(username, (res)=>{
if (username !== res.username)
return cb(new Error("No user corresponding to the cookie's email address"));
return cb(null, res);
})
});
module.exports = {
cookie : COOKIE_SECRET,
passport : passport
}