-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
33 lines (28 loc) · 1.01 KB
/
auth.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
const bcrypt = require('bcrypt-nodejs');
const LocalStrategy = require('passport-local').Strategy
module.exports = function (passport) {
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
global.db.findUserById(id, function (err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy(
(username, password, done) => {
global.db.findUserByName(username, (err, user) => {
if (err) { return done(err) }
// usuário inexistente
if (!user) { return done(null, false) }
// comparando as senhas
bcrypt.compare(password, user.password, (err, isValid) => {
if (err) { return done(err) }
if (!isValid) { return done(null, false) }
return done(null, user)
})
return done(null, user);
})
}
));
}