gpt4 book ai didi

javascript - Passport.js 不适用于 sails 套接字环境

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:09 25 4
gpt4 key购买 nike

我认为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();
});
});
};

关于javascript - Passport.js 不适用于 sails 套接字环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com