Note that there are some explanatory texts on larger screens.

plurals
  1. POHow access session data (of node-client-sessions) on socket.io?
    primarykey
    data
    text
    <p>Session data in my app is handled with <a href="https://npmjs.org/package/client-sessions" rel="nofollow">node-client-sessions</a> (as suggested in <a href="https://hacks.mozilla.org/2012/12/using-secure-client-side-sessions-to-build-simple-and-scalable-node-js-applications-a-node-js-holiday-season-part-3/" rel="nofollow">Using secure client-side sessions to build simple and scalable Node.JS applications</a>) and now I have <a href="https://npmjs.org/package/socket.io" rel="nofollow">socket.io</a> (websockets) to handle some real-time features. I would like to authenticate logged in users by their session data on handshake. But socket.io, on handshake, gives us this <a href="https://github.com/LearnBoost/socket.io/wiki/Authorizing#handshaking" rel="nofollow"><code>handshakeData</code></a> object and it doesn't expose the request object completely. I need to access the session property of the request. Any ideas?</p> <p>I'm using <a href="http://expressjs.com/" rel="nofollow">expressjs</a> (nodejs).</p> <p><strong>UPDATE</strong></p> <p>Inspired @LaurentPerrin's answer I dove into client-sessions source and found out decode and encode functions exposed. Perhaps not as easy as mess with my session object directly, but very effective so far.</p> <p>My code, so far:</p> <pre><code>/*jslint node: true, es5: true, nomen: true, unparam: true */ 'use strict'; var encode = require('client-sessions').util.encode, decode = require('client-sessions').util.decode, cookie = require('cookie'), cookieSettings = require('./persistors/cookieSettings'), authorized = { 'me@domain.com': 1 }; module.exports = function websocket(io) { io.set('authorization', function (handshakeData, callback) { var session_data; if (!handshakeData.headers.cookie) { callback({ status: 'forbidden', reason: 'no session', source: 'socket_io' }, false); return; } session_data = decode(cookieSettings, cookie.parse(handshakeData.headers.cookie).session).content; if (authorized[session_data.email]) { handshakeData.session_data = session_data; callback(null, true); } else { // callback({ // status: 'forbidden', // reason: 'unauthorized', // source: 'socket_io' // }, false); callback(null, false); } return; }); io.set('transports', ['websocket', 'flashsocket']); io.sockets.on('connection', function (socket) { console.log('connected') socket.on('credenciamento', function (data) { console.log(socket.handshake.session_data.email); socket.broadcast.emit('credenciamento', { inscrito_id: data.inscrito_id }); }); }); }; </code></pre> <p>It's by no means complete. But the authorization parts seems to be working just fine. =)</p> <p>Now, about the callback function. At first I used it like @LaurentPerrin, passing an object with the false response. But it generates a warning. Socket.io considers that an error ocurred:</p> <p><code>warn - handshake error [object Object]</code></p> <p>So in the second moment I passed <code>null</code> as first parameter when not authorizing:</p> <p><code>info - handshake unauthorized</code></p> <p>Considering the way socket.io understands each thing, the second way seems to be more correct semantically. =)</p> <p>I choose the second way. Pick one yourself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload