-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
52 lines (48 loc) · 1.64 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('./models/user');
var redis = require('redis');
var cache = redis.createClient();
var winston = require('winston');
var REDIS_USER_EXPIRATION_SECONDS = 21600; // 6 Hrs
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ name: username }, function(err, user) {
if (err) return done(err);
if (!user) {
return done(null, false, 'Incorrect username');
}
user.comparePassword(password, function(err, isMatch){
if (err) return done(err);
if (!isMatch){
done(null, false, 'Incorrect password');
} else {
done(null, { id: user.id, name: user.name });
}
});
});
}
));
passport.serializeUser(function(user, done) {
cache.setex('user:'+user.id, REDIS_USER_EXPIRATION_SECONDS, JSON.stringify({ id: user.id, name: user.name }));
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
cache.get('user:'+id, function(err, result){
if (err){
winston.error('Redis, deserializeUser: '+ err.stack);
} else if (!result) {
winston.debug('Redis, deserializeUser: empty get from db');
User.findById(id, function(err, user) {
if (!err) {
cache.setex('user:'+id, REDIS_USER_EXPIRATION_SECONDS, JSON.stringify({ id: user.id, name: user.name }));
}
done(err, { id: user.id, name: user.name });
});
} else {
winston.debug('Redis, deserializeUser: cool get from redis');
winston.debug('Result: ' + result);
done(null, JSON.parse(result));
}
})
});