如何使用护照验证套接字

时间:2014-01-31 10:23:00

标签: node.js mongodb sockets express passport.js

我使用passportauthenticate socket,但我却无法做到。

var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy;
var mongoose = require ('mongoose');
var passportSocketIo = require("passport.socketio");
var sessionStore = require('connect-mongo')(express);

mongoose.connect('mongodb://localhost/User');
var db = mongoose.connection;

var Schema = new mongoose.Schema({

    username:String,
    password:String,

});

//Creating model and instance of it
var user=mongoose.model('user', Schema);

app.configure(function() {
  app.use(express.static('public'));
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.session({ secret: 'Rchat',
  store: new sessionStore({db: 'user'} ),
  auto_reconnect:   true,
  key:'mykey'
}));

app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);

});



// calling passports local strategy 
passport.use(new LocalStrategy( function(username, password, done) {
    user.findOne({ username: username }, function (err, user) {

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

 // check for incorrect username
  if (!user) {
    return done(null, false, { message: 'Incorrect username.' });
  }

  // incorrect password for the selected username
   if (user.password != password) {
      return done(null, false, { message: 'Invalid password' });
    }
  return done(null, user);

});
}
));

// requesting for login page
app.get('/login', function(req, res){
    console.log('requesting for page');
    res.sendfile(__dirname + '/Test.html');
});

// saving the value for session use
passport.serializeUser(function(user, done) {
    done(null, user);
});

//take the information stored in the session
passport.deserializeUser(function(login, done) {
    user.findById(login._id, function(err, user) {
    done(err, user);
    });
});

我知道握手完成后会调用以下函数,但如何将用户名和密码传递给护照?

// When it does handshake
io.set('authorization', passportSocketIo.authorize({
    cookieParser: express.cookieParser,
    key:         'mykey',          
    secret:      'Rchat',  
     store:       sessionStore,        
    success:     onAuthorizeSuccess,  
    fail:        onAuthorizeFail,     
}));

//data contains all the user information from the passport
function onAuthorizeSuccess(data, accept){
    console.log('successful connection to socket.io');

// The accept-callback still allows us to decide whether to
// accept the connection or not.
    accept(null, true);
}

function onAuthorizeFail(data, message, error, accept){
    if(error)
        throw new Error(message);
        console.log('failed connection to socket.io:', message);

    // We use this callback to log all of our failed connections.
        accept(null, false);
}

// socket connection
    io.sockets.on('connection',function(socket){
            socket.on('new user',function(data, callback){

我收到错误:

错误:在集合上设置TTL索引时出错:会话

有人可以帮帮我吗?代码是否合适?谢谢!

0 个答案:

没有答案