-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpassport.js
32 lines (30 loc) · 895 Bytes
/
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
const LocalStrategy = require("passport-local");
const User = require("./models/User");
const bcrypt = require("bcryptjs");
module.exports = passport => {
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
done(null, user);
});
passport.use(
new LocalStrategy(async (username, password, done) => {
User.findOne({ username: username }, (err, user) => {
if (err) return done(err);
if (!user) return done(null, false);
bcrypt.compare(password, user.password, (err, result) => {
if (err) return done(null, false);
if (result) {
user = user.toObject();
delete user.password;
delete user.__v;
return done(null, user);
} else {
return done(null, false);
}
});
});
})
);
};