forked from jonathanschimpf/Table-Turner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passportConfig.js
35 lines (34 loc) · 1.12 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
const User = require("./models/user");
const bcrypt = require("bcryptjs");
const localStrategy = require('passport-local').Strategy;
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) {
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 = {
username: user.username,
title: user.title,
id: user._id
};
cb(err, userInformation)
});
});
};