我使用password.js在我的nodejs + express.js应用程序上处理身份验证。我设置了一个LocalStrategy以从mongodb吸引用户
我的问题是,当我重新启动节点服务器时,用户必须重新进行身份验证。这是一个问题,因为我正在积极开发它,并且不想在每次重新启动时都登录...(+我使用节点管理器)
这是我的应用设置:
app.configure(function(){
app.use('/static', express.static(__dirname + '/static'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret:'something'}));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
和会话序列化设置:
passport.serializeUser(function(user, done) {
done(null, user.email);
});
passport.deserializeUser(function(email, done) {
User.findOne({email:email}, function(err, user) {
done(err, user);
});
});
我尝试使用connect-mongodb在博客上给出的解决方案(删除了链接,因为不再存在该链接)没有成功
app.use(express.session({
secret:'something else',
cookie: {maxAge: 60000 * 60 * 24 * 30}, // 30 days
store: MongoDBStore({
db: mongoose.connection.db
})
}));
编辑其他问题:仅应建立一个连接(使用一项connexion有限的mongohq免费服务)
EDIT 2解决方案(作为我的一个版本,我的声誉现在很低,无法回答我的问题
这是我最终发现的解决方案,使用猫鼬启动的连接
app.use(express.session({
secret:'awesome unicorns',
maxAge: new Date(Date.now() + 3600000),
store: new MongoStore(
{db:mongoose.connection.db},
function(err){
console.log(err || 'connect-mongodb setup ok');
})
}));