2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-12 22:00:11 +02:00
Authenticate socket.io incoming connections with JWTs. https://www.npmjs.com/package/@thream/socketio-jwt
Go to file
2013-04-23 09:34:36 +06:00
lib Update lib/index.js 2013-02-15 23:49:56 +13:00
test add missing file 2013-02-05 19:15:17 -03:00
.gitignore initial 2012-09-05 15:14:36 -03:00
package.json Fixed devDependencies 2013-04-23 09:34:36 +06:00
README.md fix #6 use same parameters than express.session 2013-02-05 19:15:04 -03:00

Access Passport.js user information from socket.io connection.

Installation

npm install passport.socketio

Usage


  //configure passport and express

  var socketIo = require("socket.io"),
    passportSocketIo = require("passport.socketio");

  var sio = socketIo.listen(webServer);


  //except for the optional fail and success the parameter object has the 
  //same attribute than the session middleware http://www.senchalabs.org/connect/middleware-session.html

  sio.set("authorization", passportSocketIo.authorize({
    key:    'express.sid',       //the cookie where express (or connect) stores its session id.
    secret: 'my session secret', //the session secret to parse the cookie
    store:   mySessionStore,     //the session store that express uses
    fail: function(data, accept) {     // *optional* callbacks on success or fail
      accept(null, false);             // second param takes boolean on whether or not to allow handshake
    },
    success: function(data, accept) {
      accept(null, true);
    }
  }));

  sio.sockets.on("connection", function(socket){
    console.log("user connected: ", socket.handshake.user.name);
    
    //filter sockets by user...
    var userGender = socket.handshake.user.gender, 
        opposite = userGender === "male" ? "female" : "male";

    passportSocketIo.filterSocketsByUser(sio, function (user) {
      return user.gender === opposite;
    }).forEach(function(s){
      s.send("a " + userGender + " has arrived!");
    });

  });

Develop

npm install npm test

License

MIT - José F. Romaniello 2012.