forked from asm-products/octobox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.js
50 lines (45 loc) · 1.05 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
46
47
48
49
50
'use strict';
var mongoose = require('mongoose'),
LocalStrategy = require('passport-local').Strategy,
User = mongoose.model('User');
module.exports = function(passport) {
// Serialize the user id to push into the session
passport.serializeUser(function (user, done) {
done(null, user.id);
});
// Deserialize the user object based on a pre-serialized token
// which is the user id
passport.deserializeUser(function (id, done) {
User.findOne({
_id: id
}, '-salt -hashed_password', function (err, user) {
done(err, user);
});
});
// Local Strategy
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function (email, password, done) {
User.findOne({
email: email
}, function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'Unknown user'
});
}
if (!user.authenticate(password)) {
return done(null, false, {
message: 'Invalid password'
});
}
return done(null, user);
});
}
));
};