-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport-config.js
41 lines (38 loc) · 1.11 KB
/
passport-config.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
const LocalStrategy = require("passport-local").Strategy;
const User = require("./models/user");
const bcrypt = require("bcrypt");
function initialize(passport) {
const authenticateUser = (email, password, done) => {
const getUserByEmail = email => {
return User.findOne({ email: email });
};
getUserByEmail(email)
.then(user => {
if (!user) {
return done(null, false, { message: "No user with that email" });
}
bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) {
return done(err);
}
if (isMatch) {
return done(null, user);
}
return done(null, false, { message: "Password incorrect" });
});
})
.catch(err => {
return done(err);
});
};
passport.use(new LocalStrategy({ usernameField: "email" }, authenticateUser));
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(null, user);
});
});
}
module.exports = initialize;