-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
35 lines (28 loc) · 922 Bytes
/
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
34
35
const bcrypt = require('bcrypt');
const passport = require('passport');
const User = require('./models/User');
const LocalStrategy = require('passport-local');
function init() {
return new LocalStrategy((username, password, done) => {
User.findOne({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(err);
if(!result) return done(null, false);
return done(null, user);
});
});
});
}
passport.serializeUser((user, done) => {
done(null, user._id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
if(err) return done(err);
if(!user) return done(null, false);
done(null, user);
});
});
module.exports.init = init;