在express.io app的http和websocket部分之间共享护照身份验证

时间:2015-08-06 16:59:18

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

我有通行证 HTTP身份验证:

var express = require('express.io');
var passport = require('passport');
var expressSession = require('express-session');

var app = express().http().io();
app.use(express.cookieParser());
app.use(expressSession({ secret: 'someSecretWord' }));
app.use(passport.initialize());
app.use(passport.session());
router.post("/login", passport.authenticate("local", {
  successRedirect: "/index"
}));

//It works fine like this:
router.get("/index", authMiddleware.isAuthenticatedUser, function (req, res) {
    res.render("index");
});

//But doesn't affect WebSocket, so this won't work:
app.io.route('getMessage', authMiddleware.isAuthenticatedUser, function (req) {
  controller.getMessage(req).then(function (message) {
    req.io.respond(message);
  });
});

在HTTP和WebSocket请求之间共享身份验证的最佳方法是什么?

0 个答案:

没有答案