我认为passport.js是一个很棒的框架。不幸的是,它似乎不支持套接字。我之所以说这是因为Sails框架提供了http和套接字。当用户通过passport.js连接user的服务时,这并不重要。通过套接字进行访问会出错。因为套接字可能不支持中间件?
无论如何,关键的问题是,我不知道如何在套接字上应用passport.js。
发布于 2015-07-13 13:33:54
实际上,websocket请求不传递引发的护照中间件,但是可以使用解决办法。你在用这个帆护照发生器吗?
我将此代码添加到护照策略中,以将护照方法添加到套接字请求中。
/** 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();
});
});
};发布于 2016-09-08 17:32:04
亚历克西斯给出了正确的答案。我认为这是迈克所指使的方式,关于这个信息:https://stackoverflow.com/a/17793954/6793876
只需删除config/http.js中提到的护照,就可以创建具有以下内容的新策略passportMiddleware.js:
//passportMiddleware.js
var passport = require('passport');
var http = require('http');
module.exports = function (req, res, next) {
// Initialize Passport
passport.initialize()(req, res, function () {
// Use the built-in sessions
passport.session()(req, res, function () {
res.locals.user = req.user;
var methods = ['login', 'logIn', 'logout', 'logOut', 'isAuthenticated', 'isUnauthenticated'];
if (req.isSocket) {
for (var i = 0; i < methods.length; i++) {
req[methods[i]] = http.IncomingMessage.prototype[methods[i]].bind(req);
}
}
next();
});
});
};最后,在policies.js中将此策略添加到所有控制器中:
module.exports.policies = {
RabbitController: {
nurture : ['passportMiddleware','isRabbitMother'],
feed : ['passportMiddleware','isNiceToAnimals', 'hasRabbitFood']
}
};https://stackoverflow.com/questions/31373595
复制相似问题