我认为passport.js是一个了不起的框架。但不幸的是,它似乎不支持套接字。为什么我这么说是因为Sails框架提供了http和socket。当用户通过passport.js连接sails的服务时,这并不重要。通过套接字访问会出错。因为socket可能不支持中间件?
无论如何,关键问题是,我不知道如何在套接字上应用passport.js。
事实上,websocket 请求不会通过护照中间件,但可以使用解决方法。你在用这个sails passport generator ?
我将此代码添加到护照策略中,以将护照方法添加到套接字请求中。
/** Content not generated BEGIN */
var http = require('http')
, methods = ['login', 'logIn', 'logout', 'logOut', 'isAuthenticated', 'isUnauthenticated'];
/** Content not generated END */
module.exports = function (req, res, next) {
// Initialize Passport
passport.initialize()(req, res, function () {
// Use the built-in sessions
passport.session()(req, res, function () {
// Make the user available throughout the frontend
res.locals.user = req.user;
/** Content not generated BEGIN */
// Make the passport methods available for websocket requests
if (req.isSocket) {
for (var i = 0; i < methods.length; i++) {
req[methods[i]] = http.IncomingMessage.prototype[methods[i]].bind(req);
}
}
/** Content not generated END */
next();
});
});
};
我是一名优秀的程序员,十分优秀!