-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpassport.js
45 lines (36 loc) · 1.08 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
(function() {
var LocalStrategy, bcrypt, db, passport;
db = require("redis").createClient();
bcrypt = require('bcrypt');
passport = require('passport');
LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) {
return done(null, user.username);
});
passport.deserializeUser(function(username, done) {
return db.hgetall("user:" + username, function(err, user) {
return done(null, user);
});
});
passport.use(new LocalStrategy(function(username, password, done) {
return db.hget("user:" + username, 'password', function(err, hash) {
if (err) {
return done(err);
}
if (hash) {
return bcrypt.compare(password, hash, function(err, match) {
if (match) {
return db.hgetall("user:" + username, function(err, user) {
return done(null, user);
});
} else {
return done(null, false);
}
});
} else {
return done(null, false);
}
});
}));
module.exports = passport;
}).call(this);