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

标签 javascript node.js sails.js

我认为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/

相关文章:

javascript - MomentJS 如何使用 > 和 <(和 >=、<=)比较值?

node.js - 如何在 digital ocean 中使用 nginx 和 pm2 运行两个独立的 Nodejs 应用程序

node.js - 如何使用 gulp-tap 将文件名传递给 Gulp 管道中的下一个操作?

javascript - 在 Node.js 中获取 Excel 工作表单元格的计算值

JavaScript:连续调用对象上的函数列表

javascript - 如何在 jsreport 的图表中使用自定义数据?

javascript - 从字节数组加载图像内容

node.js - is-port-reachable 使用 promises 在循环中获取结果

node.js - 包括在 Sails.js 中查看特定 Assets

javascript - Sails.js Waterline 查询(按关联)