0

我刚刚配置了我的 express (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 socket.io@1.X 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 中,我的快速应用程序说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);
                }
            });
        })
    }
};

我的 redis 商店看起来像

{"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":"filip.lukac@gmail.com",
"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

我无法弄清楚,问题出在哪里......有人可以帮助我吗?

我只想看看我的用户是否经过身份验证

4

1 回答 1

0

编辑:

我曾经创建拉请求。如果有人在使用 RedisStore,请检查一下。

拉取请求

问题出在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() 解决。

于 2014-09-21T11:04:09.670 回答