将会话写入MongoDB时出错

时间:2016-09-14 02:42:27

标签: node.js mongodb storage passport.js middleware

我正在开发一个节点应用并使用MongoDB来存储会话。该应用程序使用Linkedin oAuth和MongoDB。

我的代码:

var store = new MongoDBStore({
    uri: mongoUri,
    collection: '_sessions',
    function(err) {
        console.log(err);
    }
});

// Catch MONGO CONNECTION errors
store.on('error', function(error) {
    assert.ifError(error);
    assert.ok(false);
});

// if (isLocal) {
//     app.use(session({resave: 'true', saveUninitialized: 'true', secret: 'keyboardcat'}));
// }else{
// }
app.use(session({
    store: store,
    resave: 'true',
    saveUninitialized: 'true',
    secret: process.env.SESSION_SECRET,
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7
    }
}));

应用启动时出现以下错误。

Error: Error setting ZwkuhpnbkYgD1HzscBJ8w2fmhwZpiQPL to Session {
  cookie: 
   { path: '/',
     _expires: 2016-09-20T22:20:15.753Z,
     originalMaxAge: 604800000,
     httpOnly: true,
     secure: null,
     domain: null },
  passport: {},
  'oauth2:www.linkedin.com': { state: 'DqyHf7eqG8RCN7BYRkG9NSUV' } }: The dotted field 'oauth2:www.linkedin.com' in 'session.oauth2:www.linkedin.com' is not valid for storage.
    at /Users/vc/Sites/HavasCognitiveID/node_modules/connect-mongodb-session/index.js:147:19
    at handleCallback (/Users/vc/Sites/HavasCognitiveID/node_modules/connect-mongodb-session/node_modules/mongodb/lib/utils.js:96:12)
    at /Users/vc/Sites/HavasCognitiveID/node_modules/connect-mongodb-session/node_modules/mongodb/lib/collection.js:1008:42
    at handler (/Users/vc/Sites/HavasCognitiveID/node_modules/mongodb-core/lib/topologies/replset.js:414:5)[nodemon] restarting due to changes...

据我所知,mongodb抱怨虚线字段。我的问题是如何在用户通过身份验证之后拦截会话:{}?

1 个答案:

答案 0 :(得分:0)

您可以覆盖定义“ LinkedIn”策略时将其添加为选项的“ sessionKey”

passport.use(新的LinkedinStrategy({... options,sessionKey:'linkedin'},strategy.linkedin));