Socket.io authentication middleware using Json Web Token
npm install socketio-jwt-auth   
> Socket.io authentication middleware using Json Web Token
Work with socket.io >= 1.0
```
npm install socketio-jwt-auth
__socketio-jwt-auth__ has only one method authenticate(options, verify).
options is an object literal that contains options:
* secret a secret key,algorithm
* , defaults to HS256, andsucceedWithoutToken
* , which, if true tells the middleware not to fail if no token is suppled. Defaults tofalse.
verify is a function with two args payload, and done:
* payload is the decoded JWT payload, anddone
* is an error-first callback with three args: done(err, user, message)
`javascript
var io = require('socket.io')();
var jwtAuth = require('socketio-jwt-auth');
// using middleware
io.use(jwtAuth.authenticate({
secret: 'Your Secret', // required, used to verify the token's signature
algorithm: 'HS256' // optional, default to be HS256
}, function(payload, done) {
// done is a callback, you can use it as follows
User.findOne({id: payload.sub}, function(err, user) {
if (err) {
// return error
return done(err);
}
if (!user) {
// return fail with an error message
return done(null, false, 'user does not exist');
}
// return success with a user info
return done(null, user);
});
}));
`
There are times when you might wish to successfully connect the socket but indentify the connection as being un-authenticated. For example when a user connects as a guest, before supplying login credentials. In this case you must supply the option succeedWithoutToken, as follows:
`javascript
var io = require('socket.io')();
var jwtAuth = require('socketio-jwt-auth');
// using middleware
io.use(jwtAuth.authenticate({
secret: 'Your Secret', // required, used to verify the token's signature
algorithm: 'HS256', // optional, default to be HS256
succeedWithoutToken: true
}, function(payload, done) {
// you done callback will not include any payload data now
// if no token was supplied
if (payload && payload.sub) {
User.findOne({id: payload.sub}, function(err, user) {
if (err) {
// return error
return done(err);
}
if (!user) {
// return fail with an error message
return done(null, false, 'user does not exist');
}
// return success with a user info
return done(null, user);
});
} else {
return done() // in your connection handler user.logged_in will be false
}
}));
`
javascript
io.on('connection', function(socket) {
console.log('Authentication passed!');
// now you can access user info through socket.request.user
// socket.request.user.logged_in will be set to true if the user was authenticated
socket.emit('success', {
message: 'success logged in!',
user: socket.request.user
});
});io.listen(9000);
`$3
`javascript
`If your client support, you can also choose to pass the auth token in headers.
`javascript
`Tests
`
npm install
npm test
`Change Log
$3
* Fix a bug caused by undefined
$3
* Add auth handshake for Socket.IO v3
$3
* Add support for passing auth token with
extraHeaders$3
* Fix an api bug of
node-simple-jwt$3
* Add an option (
succeedWithoutToken`) to allow guest connectionCopyright (c) 2015 Lei Lei