4

I'm working on a Node.js application where pretty much all the communication is taking place via sockets.io.

What I need to do is, before processing any request (other than the login request) ensure that the user is authenticated.

The obvious way to do this would be to have a catch-all listener which is called prior to the execution of any method.

I can't seem to find anything like this in Socket.io, though. How can I achieve is? Is there a better approach than the one I'm taking?

NRaf
  • 7,407
  • 13
  • 52
  • 91
  • I understand the problem, i passing by the same situation, the answer given bellow not solve the problem, i guess that you are creating a single page application and as you don't have a reload on the page, the handshake occours just one time... – Guilherme Oliveira Feb 28 '12 at 20:40

2 Answers2

1

The best approach here would be to authenticate the user upon connection (handshake), by parsing the cookie and decoding the session.

Read more about this in the following links:

http://www.danielbaulig.de/socket-ioexpress/ (this contains a detailed tutorial of everything you need to do)
https://github.com/LearnBoost/socket.io/wiki/Authorizing
socket.io and session?

Community
  • 1
  • 1
alessioalex
  • 62,577
  • 16
  • 155
  • 122
0

Checkout out socket.io-events on nom for "Catching all events"

var router = requrie('socket.io-events')();
router.on('*', function (sock, args, cb) {
  //do something with the args!
  cb();
});
var io = require('socket.io')(3000);

Checkout out socket.io-handshake on nom for "socket.io v1.x session support"

Nathan Romano
  • 7,016
  • 3
  • 19
  • 18