passport-socketio商店属性配置

时间:2013-09-09 16:05:59

标签: node.js express socket.io passport.js

我想知道如何配置passport.socketio

我阅读了文档,但仍然可以介绍如何设置store属性。

我正在使用passport-local

这是我的代码(相关部分):

一些配置:

app.use( express.cookieParser() );
app.use(express.methodOverride());
// use express.session before passport, so that passport session will work
app.use(express.session({ secret: 'somerandomcod3' }));
// Initialize Passport!  Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());

passport.socketio config

  io.set("authorization", passportSocketIo.authorize({
    cookieParser: express.cookieParser, 
    key:          'express.sid',        
    secret:       'somerandomcod3',  
    store:         express.session,      // not working tried also a couple others
    fail: function(data, accept) { 
      accept(null, false);           
    },
    success: function(data, accept) {

      accept(null, true);
    }
  }));

我的本地策略

var LocalStrategy = require('passport-local').Strategy ;

passport.use(new LocalStrategy(function(username, password, done) {

  db.userModel.findOne({ username: username }, function(err, user) {
    if (err) { return done(err); }
    if (!user) { return done(null, false, { message: 'Unknown user ' + username }); }
    user.comparePassword(password, function(err, isMatch) {

      if (err) {

        return done(err);
      }
      if(isMatch) {

        return done(null, user);
      } else {

        return done(null, false, { message: 'Invalid password' });
      }
    });
  });
}));

所以问题是什么或更好的我如何获得会话存储。 ?!

1 个答案:

答案 0 :(得分:1)

商店可以是多个值。默认情况下,商店是MemoryStore,如果您想要准确的参考,则为express.session.MemoryStore

MemoryStore非常适合无依赖性会话,但它们的缺点是,如果重新启动节点进程,则会丢失所有会话。基本上这对于任何生产构建都很糟糕,所以如果这是一个严肃的应用程序,你应该看看this question about different session stores

我认为代码的相关部分应该是:

var express = require("express")
  , app = express()
  , sessionStore = new express.session.MemoryStore(); //or some other kind of store

app.use(express.session({ secret: 'somerandomcod3', store: sessionStore}));

io.set("authorization", passportSocketIo.authorize({
    cookieParser: express.cookieParser 
  , key: 'express.sid'   
  , secret: 'somerandomcod3'
  , store: sessionStore
  , fail: function(data, accept) { 
     accept(null, false);           
    }
  , success: function(data, accept) {
     accept(null, true);
   }
}));

希望你现在有了更好的理解:)