Node.js + express.js +passport.js:在服务器重启之间保持身份验证 [英] Node.js + express.js + passport.js : stay authenticated between server restart

查看:30
本文介绍了Node.js + express.js +passport.js:在服务器重启之间保持身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用passport.js 来处理我的nodejs + express.js 应用程序上的身份验证.我设置了一个 LocalStrategy 来从 mongodb 中获取用户

I use passport.js to handle auth on my nodejs + express.js application. I setup a LocalStrategy to take users from mongodb

我的问题是当我重新启动节点服务器时,用户必须重新进行身份验证.这是一个问题,因为我正在积极开发它并且不想在每次重新启动时登录...(+ 我使用节点主管)

My problems is that users have to re-authenticate when I restart my node server. This is a problem as I am actively developing it and don't wan't to login at every restart... (+ I use node supervisor)

这是我的应用设置:

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尝试了博客上给出的解决方案(删除链接,因为它不再存在)没有成功

I tried the solution given on a blog (removed the link as it does not exist any more) using connect-mongodb without success

app.use(express.session({
    secret:'something else',
    cookie: {maxAge: 60000 * 60 * 24 * 30}, // 30 days
        store: MongoDBStore({
        db: mongoose.connection.db
    })
}));

EDIT 附加问题:只应建立一个连接(使用一个连接受限的 mongohq 免费服务)

EDIT additional problem : only one connection should be made (use of one connexion limited mongohq free service)

EDIT 2 解决方案(作为一个版本,我现在的声誉很低,无法回答我的问题

EDIT 2 solution (as an edition as I my reputation is to low to answer my question by now

这是我最终找到的解决方案,使用猫鼬发起的连接

Here is the solution I finally found, using mongoose initiated connection

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');
        })
}));

推荐答案

有一个名为 connect 的开源-mongo 完全符合您的需求 - 将会话数据保存在 mongodb 中

There's an opensource called connect-mongo that does exactly what you need - persists the session data in mongodb

使用示例(重用 mongoose 打开的连接):

usage example (with a reuse of mongoose opened connection) :

var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/sess');
app.use(express.session({
    secret:'secret',
    maxAge: new Date(Date.now() + 3600000),
    store: new MongoStore(
    // Following lines of code doesn't work
    // with the connect-mongo version 1.2.1(2016-06-20).
    //    {db:mongoose.connection.db},
    //    function(err){
    //        console.log(err || 'connect-mongodb setup ok');
    //   }
    {mongooseConnection:mongoose.connection}
    )        
}));

您可以在这里阅读更多相关信息:https://github.com/kcbanner/connect-mongo

you can read more about it here: https://github.com/kcbanner/connect-mongo

这篇关于Node.js + express.js +passport.js:在服务器重启之间保持身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆