2014-09-11 65 views
0

我正在使用loopback 2.0和socket.io 1.0.6。使用環回令牌認證來認證socket.io

我想使用環回驗證方法來驗證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()方法。我應該怎麼做?

回答

0

loopback.token是一個快速中間件,它期望第一個參數是一個快速請求。