passport.js 不适用于风帆套接字环境 [英] passport.js doesn't work on sails socket environment
问题描述
我认为passport.js 是一个了不起的框架.然而不幸的是,它似乎不支持套接字.之所以这么说,是因为 Sails 框架提供了 http 和 socket.当用户通过passport.js 连接sails 的服务时,没有关系.通过套接字访问会出错.因为socket可能不支持中间件?
I think passport.js is a amazing framework. Unfortunately, however, it seems to be that doesn't support socket. Why I said this is that Sails framework provides http and socket. When user connected sails's service through passport.js, it doesn't matter. Accessing by socket makes error. Because socket may not support middleware?
无论如何,关键问题,我不知道如何在socket上应用passport.js.
Anyway, the critical problem, I don't know how apply passport.js on socket.
推荐答案
确实,websocket 请求没有通过抛出了passport中间件,但是可以使用一种解决方法.您在使用这个 sails 护照生成器吗?
Indeed, the websocket requests do not pass threw the passport middleware, but it is possible to use a workaround. Are you using this sails passport generator ?
我将此代码添加到通行证策略中,以向套接字请求添加通行证方法.
I added this code to the passport policy to add passport methods to the socket requests.
/** 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();
});
});
};
这篇关于passport.js 不适用于风帆套接字环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!