-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpassportConfig.js
38 lines (35 loc) · 1004 Bytes
/
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 bcrypt = require("bcryptjs");
const localStrategy = require("passport-local").Strategy;
const User = require("./models/user");
module.exports = function (passport) {
passport.use(
new localStrategy((username, password, done) => {
User.findOne({ username: username }, (err, user) => {
if (err) throw err;
if (!user) return done(null, false);
bcrypt.compare(password, user.password, (err, result) => {
if (err) throw err;
if (result === true) {
return done(null, user);
} else {
return done(null, false);
}
});
});
})
);
passport.serializeUser((user, cb) => {
cb(null, user.id);
});
passport.deserializeUser((id, cb) => {
User.findOne({ _id: id }, (err, user) => {
const userInformation = {
id: user.id,
username: user.username,
admin: user.admin,
phone: user.phone,
};
cb(err, userInformation);
});
});
};