socketio-auth
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -15,2 +15,3 @@ 'use strict'; | ||
* @param {Function} config.postAuthenticate=noop - called after the client is authenticated | ||
* @param {Function} config.disconnect=noop - called after the client is disconnected | ||
* @param {Number} [config.timeout=1000] - amount of millisenconds to wait for a client to | ||
@@ -23,2 +24,3 @@ * authenticate before disconnecting it. A value of 'none' means no connection timeout. | ||
var postAuthenticate = config.postAuthenticate || _.noop; | ||
var disconnect = config.disconnect || _.noop; | ||
@@ -58,2 +60,6 @@ _.each(io.nsps, forbidConnections); | ||
socket.on('disconnect', function() { | ||
return disconnect(socket); | ||
}); | ||
if (timeout !== 'none') { | ||
@@ -60,0 +66,0 @@ setTimeout(function() { |
{ | ||
"name": "socketio-auth", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Authentication for socket.io", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -25,9 +25,9 @@ # socketio-auth [![Build Status](https://secure.travis-ci.org/facundoolano/socketio-auth.png)](http://travis-ci.org/facundoolano/socketio-auth) | ||
var password = data.password; | ||
db.findUser('User', {username:username}, function(err, user) { | ||
//inform the callback of auth success/failure | ||
if (err || !user) return callback(new Error("User not found")); | ||
return callback(null, user.password == password); | ||
} | ||
}); | ||
} | ||
@@ -47,4 +47,5 @@ }); | ||
require('socketio-auth')(io, { | ||
authenticate: authenticate, | ||
authenticate: authenticate, | ||
postAuthenticate: postAuthenticate, | ||
disconnect: disconnect, | ||
timeout: 1000 | ||
@@ -62,7 +63,7 @@ }); | ||
var password = data.password; | ||
db.findUser('User', {username:username}, function(err, user) { | ||
if (err || !user) return callback(new Error("User not found")); | ||
return callback(null, user.password == password); | ||
} | ||
}); | ||
} | ||
@@ -75,9 +76,16 @@ ``` | ||
var username = data.username; | ||
db.findUser('User', {username:username}, function(err, user) { | ||
socket.client.user = user; | ||
} | ||
}); | ||
} | ||
``` | ||
* `disconnect`: a function to be called after the client is disconnected. | ||
```javascript | ||
function disconnect(socket) { | ||
console.log(socket.id + ' disconnected'); | ||
} | ||
``` | ||
* `timeout`: The amount of millisenconds to wait for a client to authenticate before disconnecting it. Defaults to 1000. The value 'none' disables the timeout feature. | ||
@@ -92,7 +100,7 @@ | ||
socket.on('unauthorized', function(err){ | ||
console.log("There was an error with the authentication:", err.message); | ||
console.log("There was an error with the authentication:", err.message); | ||
}); | ||
``` | ||
The value of `err.message` depends on the outcome of the `authenticate` function used in the server: if the callback receives an error its message is used, if the success parameter is false the message is `'Authentication failure'` | ||
The value of `err.message` depends on the outcome of the `authenticate` function used in the server: if the callback receives an error its message is used, if the success parameter is false the message is `'Authentication failure'` | ||
@@ -106,6 +114,6 @@ ```javascript | ||
} | ||
//if wrong password err.message will be "Authentication failure" | ||
return callback(null, user.password == data.password); | ||
} | ||
return callback(null, user.password == data.password); | ||
}); | ||
} | ||
@@ -112,0 +120,0 @@ ``` |
@@ -201,1 +201,29 @@ 'use strict'; | ||
}); | ||
describe('Server socket disconnect', function() { | ||
var server; | ||
var client; | ||
it('Should call discon function', function(done) { | ||
server = new ServerSocketMock(); | ||
client = new ClientSocketMock(5); | ||
var discon = function(socket) { | ||
assert.equal(socket, client); | ||
done(); | ||
}; | ||
require('../lib/socketio-auth')(server, { | ||
timeout:80, | ||
authenticate: authenticate, | ||
disconnect: discon | ||
}); | ||
server.connect('/User', client); | ||
process.nextTick(function() { | ||
client.disconnect(); | ||
}); | ||
}); | ||
}); |
16666
267
125