我一直在本地与Couch和Redis一起构建Express应用程序。它一直很好。
我现在正在寻求使用Iris Redis上托管的远程Redis服务器。我已经尝试了好几天了但是在使用Passport的本地策略进行身份验证后,似乎无法通过Express'Connect-redis返回cookie。当我运行本地Redis服务器时,这工作得很好。
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use('/upload', upload.fileHandler());
app.use(express.bodyParser({ keepExtensions: true, uploadDir: 'public/uploads' }));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ cookie: { maxAge: 60000000 }, secret: "sauce", store: new RedisStore({
port: 6379,
host: 'url',
pass: 'sauce'
}) }));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// Authentication
passport.use(new LocalStrategy(function(username, password, done) {
username = username.toLowerCase();
db.get(username, function (err, doc) {
if (err) {
console.log(err);
return done(err);
}
if (!doc) {
return done(null, false, { message: 'No user by that name.' });
}
if ( doc.password !== password ) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, doc);
});
})
);
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (userId, done) {
db.get(userId, function(err, user){
if(err){
return (500, err);
} else {
done(null, _.omit(user, ["password"]));
}
});
});
// Route
exports.authenticate = passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login'
});
答案 0 :(得分:1)
您似乎没有指定redis db;这可能是失败的原因。
更一般地说,我建议在环境变量中使用URL来存储redis参数,因此无论您是连接到本地服务器还是远程服务器,都要使用相同的解析代码。例如,来自app.js:
var config = require('./config/config.js')
app.use(express.session({
store: new RedisStore({
port: config.redisPort,
host: config.redisHost,
db: config.redisDatabase,
pass: config.redisPassword}),
secret: 'pick a good password',
proxy: true, // necessary if you're behind a proxy
cookie: { secure: true }
}))
和config.js:
var url = require('url')
var config = {};
var redisUrl;
if (typeof(process.env.REDISTOGO_URL) != 'undefined') {
redisUrl = url.parse(process.env.REDISTOGO_URL);
}
else redisUrl = url.parse('redis://:@127.0.0.1:6379/0');
config.redisProtocol = redisUrl.protocol.substr(0, redisUrl.protocol.length - 1); // Remove trailing ':'
config.redisUsername = redisUrl.auth.split(':')[0];
config.redisPassword = redisUrl.auth.split(':')[1];
config.redisHost = redisUrl.hostname;
config.redisPort = redisUrl.port;
config.redisDatabase = redisUrl.path.substring(1);
console.log('Using Redis store ' + config.redisDatabase)
module.exports = config;