2014-09-20 84 views
0

我只是配置了我的快递(4.x版)+ socket.io(1.x中)+ angular.js应用和我的应用程序看起来像插座始终不被认证

快递

app.use(session({secret:"mysecret",store:new RedisStore({ host: 'localhost', port: 6379, client:  redis }), cookie: {httpOnly: false,secure: false}})); 

Socket.io配置

function onAuthorizeSuccess(data, accept){ 
    console.log('successful connection to socket.io'); 

    // If you use [email protected] the callback looks different 
    accept(); 
} 

function onAuthorizeFail(data, message, error, accept){ 
    if(error) throw new Error(message); 
    return accept(); 
} 

io.use(passportSocketIo.authorize({ 
    passport:passport, 
    cookieParser: cookieParser, 
    key:   'connect.sid',  // the name of the cookie where express/connect stores its  session_id 
    secret:  'mysecret', // the session_secret to parse the cookie 
    store:  new RedisStore({ host: 'localhost', port: 6379, client: redis }), 
    success:  onAuthorizeSuccess, // *optional* callback on success - read more below 
    fail:  onAuthorizeFail  // *optional* callback on fail/error - read more below 
})); 

一切工作正常,会话存储在Redis的,我的Express应用程序说req.isAutheticated() = true

但是:

io.sockets.on('connection', function (socket) { 
    console.log(socket.request.user); 


    socket.on('disconnect',function(){ 
     console.log('User disconnected'); 
    }); 
}); 

说:logged_in = false

我的角码的样子(的getCookie( 'connect.sid')具有正确的价值)

var socket = io.connect('ws://chipso.eu:3000/',{ 
    query: 'session_id=' + getCookie('connect.sid') 
}); 

return { 
    on: function (eventName, callback) { 
     socket.on(eventName, function() { 
      var args = arguments; 
      $rootScope.$apply(function() { 
       callback.apply(socket, args); 
      }); 
     }); 
    }, 
    emit: function (eventName, data, callback) { 
     socket.emit(eventName, data, function() { 
      var args = arguments; 
      $rootScope.$apply(function() { 
       if (callback) { 
        callback.apply(socket, args); 
       } 
      }); 
     }) 
    } 
}; 

我rediss商店看起来像

{"cookie":{"originalMaxAge":2591999999,"expires":"2014-10-20T19:06:23.097Z","secure":false, 
"httpOnly":false,"path":"/"},"passport":{"user":{"id":1,"name":"Filip Lukáč","first_name":"Filip", 
"last_name":"Lukáč","gender":"male","reg_date":"2014-09-15 11:57:34.079","username":"[email protected]", 
"role":"admin","photo":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/p100x100/ 
10645292_334651976702411_6704623329146986982_n.jpg?oh=a5ed18ae84dfc3909b4bfb036a0a2b8d& 
oe=548BC691&__gda__=1419568684_70705b96aaa90cf986992372925d442e","logged":true}}} 

这是我的调试日志。

_query: 
    { session_id: 's:uhckQqqa4XdGEtOHg0EwrumGdRDYoa9C.QEWbVG1/w3aqH7WJ97YSTisZuKlZvQd1rYJvV92L2Gs', 
    EIO: '3', 
    transport: 'polling', 
    t: '1411242792055-0' }, 
    res: 
    { domain: null, 
    _events: { finish: [Function] }, 
    _maxListeners: 10, 
    output: [], 
    outputEncodings: [], 
    writable: true, 
    _last: false, 
    chunkedEncoding: false, 
    shouldKeepAlive: true, 
    useChunkedEncodingByDefault: true, 
    sendDate: true, 
    _headerSent: true, 
    _header: 'HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nContent-Length: 101\r\nAccess-Control-Allow-Origin: *\r\nSet-Cookie: io=ET-vPrTRy078bMyiAAAD\r\nDate: Sat, 20 Sep 2014 19:53:10 GMT\r\nConnection: keep-alive\r\n\r\n', 
    _hasBody: true, 
    _trailer: '', 
    finished: true, 
    _hangupClose: false, 
    socket: null, 
    connection: null, 
    statusCode: 200 }, 
    cleanup: [Function: cleanup], 
    read: [Function], 
    socketio_version_1: true, 
    cookie: { 'connect.sid': 'uhckQqqa4XdGEtOHg0EwrumGdRDYoa9C' }, 
    sessionID: 's:uhckQqqa4XdGEtOHg0EwrumGdRDYoa9C.QEWbVG1/w3aqH7WJ97YSTisZuKlZvQd1rYJvV92L2Gs', 
    user: { logged_in: false } } 

未找到会话,{ logged_in: false }。并且我的onAuthorizeFail中的消息说= No session found

我不知道,哪里是问题...任何人都可以帮助我吗?

我只是想看看我的用户进行身份验证

回答

0

编辑:

我用来创建拉请求。如果有人在使用RedisStore,请检查这一点。

Pull-request

问题是在passport.socketio

https://github.com/jfromaniello/passport.socketio/blob/master/lib/index.js#L57 在这里,当我试图找到我的RedisStore会议。

sessionID = data.query.session_id. 

但会议由

session.cookie[auth.key]. 

存储,因此我已经重写从

data.sessionID = (data.query && data.query.session_id) || (data._query && data._query.session_id) || data.cookie[auth.key] || ''; 

此行

data.sessionID = data.cookie[auth.key] || ''; 

警告: 这可能只是解决为RedisStore ()。

+0

https://github.com/jfromaniello/passport.socketio/pull/82 – 2014-09-21 13:03:01

+0

您的“修复”仅删除了从“data.query.session_id'和'data._query.session_id'读取会话ID的选项。如果它们是空的,首先你的'sessionID'会从'data.cookie [auth.key]'接收到。 – vesse 2014-09-24 06:35:27

+0

@vesse是正确的..这可以改变你的唯一方法是如果你发送'session_id'。 – 2014-10-06 11:54:43