-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassportConfig.js
38 lines (32 loc) · 1.17 KB
/
passportConfig.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
const User = require("./models/user");
const bcrypt = require("bcryptjs");
const localStrategy = require("passport-local").Strategy;
module.exports = function (passport) {
passport.use(
new localStrategy( { usernameField: 'email' }, (email, password, done) => {
User.findOne({ email: email })
.then(user => {
if (!user) return done(null, false);
bcrypt.compare(password, user.password, (err, result) => {
if (result === true) {
return done(null, user);
} else {
return done(null, false);
}
})
});
})
);
//sirializeUser store a cookie inside the browser (the user id)
passport.serializeUser((user, callback) => {
callback(null, user._id);
})
//resotre user from cookie stored in browser and return it
passport.deserializeUser((id, callback) => {
User.findById(id)
.then(user => {
callback(null, user)
})
.catch(err => callback(err, null))
})
}