add noqs method
This commit is contained in:
29
lib/index.js
29
lib/index.js
@ -1,9 +1,30 @@
|
||||
var xtend = require('xtend');
|
||||
var jwt = require('jsonwebtoken');
|
||||
var UnauthorizedError = require('./UnauthorizedError');
|
||||
var url = require('url');
|
||||
|
||||
function authorize(options) {
|
||||
function noQsMethod(options, onConnection) {
|
||||
return function (socket) {
|
||||
var auth_timeout = setTimeout(function () {
|
||||
socket.disconnect('unauthorized');
|
||||
}, options.timeout || 5000);
|
||||
|
||||
socket.on('authenticate', function (data) {
|
||||
clearTimeout(auth_timeout);
|
||||
jwt.verify(data.token, options.secret, options, function(err, decoded) {
|
||||
if (err) {
|
||||
return socket.disconnect('unauthorized');
|
||||
}
|
||||
|
||||
socket.user = decoded;
|
||||
socket.emit('authenticated');
|
||||
onConnection(socket);
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
function authorize(options, onConnection) {
|
||||
var defaults = {
|
||||
success: function(data, accept){
|
||||
accept(null, true);
|
||||
@ -15,6 +36,10 @@ function authorize(options) {
|
||||
|
||||
var auth = xtend(defaults, options);
|
||||
|
||||
if (onConnection) {
|
||||
return noQsMethod(options, onConnection);
|
||||
}
|
||||
|
||||
return function(data, accept){
|
||||
var token, error;
|
||||
|
||||
|
Reference in New Issue
Block a user