-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
45 lines (34 loc) · 1.26 KB
/
passport.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
const passport = require('passport')
, LocalStrategy = require('passport-local').Strategy;
module.exports = function(db){
const UserDAO = require(`${appRoot}/models/dao/userdao.js`);
const User = new UserDAO(db);
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local',new LocalStrategy({
passReqToCallback : true
},
function(req,username, password, done) {
User.findOne({ email: username }, function(err, user) {
if (err) {
return done(err);
}else if (!user) {
console.log('Incorrect username');
return done(null, false, { message: 'Identifiant incorrect.' });
}else if (!user.validPassword(password)) {
console.log('Incorrect password');
return done(null, false, { message: 'Mot de passe incorrect.' });
}else{
console.log('All good my friend!');
return done(null, user);
}
});
}
));
}