使用环回令牌认证来验证socket.io

时间:2014-09-11 04:45:52

标签: strongloop socket.io-1.0

我正在使用loopback 2.0和socket.io 1.0.6。

我想使用loopback身份验证方法来验证socket.io。

我找到了在loopback/lib/middleware/token.js中对用户进行身份验证的方法。 https://github.com/strongloop/loopback/blob/master/lib/middleware/token.js

然后我写下面的内容。

var loopback = require('loopback');
var ioapp = module.exports = socketio;

function socketio(server) {
  var io = require('socket.io')(server);

  // auth
  io.use(function(socket, next) {
    loopback.token()(socket.request, null, next);
  });

  // listeners
  ...

  return io;
};

但实际上它不起作用并导致这样的错误。

/Users/.../project_root/node_modules/loopback/lib/models/access-token.js:201
    id = req.param(params[i]);
             ^
TypeError: Object #<IncomingMessage> has no method 'param'
    at tokenIdForRequest (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/loopback/lib/models/access-token.js:201:14)
    at Function.AccessToken.findForRequest (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/loopback/lib/models/access-token.js:123:12)
    at /Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/loopback/lib/middleware/token.js:53:16
    at Array.0 (/Users/ksuzuki/Projects/appsocially/repo/chat-center/server/socket.js:15:28)
    at run (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/socket.io/lib/namespace.js:114:11)
    at Namespace.run (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/socket.io/lib/namespace.js:126:3)
    at Namespace.add (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/socket.io/lib/namespace.js:155:8)
    at Client.connect (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/socket.io/lib/client.js:67:20)
    at Server.onconnection (/Users/ksuzuki/Projects/appsocially/repo/chat-center/node_modules/socket.io/lib/index.js:309:10)
    at Server.EventEmitter.emit (events.js:95:17)

我想这是因为我将错误的对象类型传递给loopback.token()方法。我该怎么办?

1 个答案:

答案 0 :(得分:0)

loopback.token是一个快速中间件,它希望第一个参数是一个快速请求。