2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00
Authenticate socket.io incoming connections with JWTs. https://www.npmjs.com/package/@thream/socketio-jwt
Go to file
José F. Romaniello 3675d24adf Merge pull request #2 from bradleyolson/master
Success and Fail Callbacks
2012-10-27 07:03:21 -07:00
lib added option of success or fail callbacks. Needed to still allow users access to sockets even if they weren't logged in, but needed specific data if they were. 2012-10-26 11:13:28 -05:00
.gitignore initial 2012-09-05 15:14:36 -03:00
package.json update version 2012-10-04 14:55:27 -03:00
README.md updated readme to reflect changes. 2012-10-26 11:19:01 -05: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);

  sio.set("authorization", passportSocketIo.authorize({
    sessionKey:    'express.sid',      //the cookie where express (or connect) stores its session id.
    sessionStore:  mySessionStore,     //the session store that express uses
    sessionSecret: "my session secret", //the session secret to parse the cookie
    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!");
    });

  });

License

MIT - José F. Romaniello 2012.