Merge branch 'dbrugne-master'
This commit is contained in:
commit
de40574362
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
.idea
|
||||
node_modules/*
|
38
lib/index.js
38
lib/index.js
@ -27,29 +27,39 @@ function noQsMethod(options) {
|
||||
if(options.required){
|
||||
clearTimeout(auth_timeout);
|
||||
}
|
||||
|
||||
jwt.verify(data.token, options.secret, options, function(err, decoded) {
|
||||
var onError = function(){
|
||||
return socket.disconnect('unauthorized');
|
||||
// error handler
|
||||
var onError = function(err, code) {
|
||||
if (err) {
|
||||
code = code || 'unknown';
|
||||
var error = new UnauthorizedError(code, {
|
||||
message: (Object.prototype.toString.call(err) === '[object Object]' && err.message) ? err.message : err
|
||||
});
|
||||
socket.emit('unauthorized', error, function() {
|
||||
socket.disconnect('unauthorized');
|
||||
});
|
||||
return; // stop logic, socket will be close on next tick
|
||||
}
|
||||
};
|
||||
|
||||
if (err) {
|
||||
onError();
|
||||
return onError(err, 'invalid_token');
|
||||
}
|
||||
|
||||
// success handler
|
||||
var onSuccess = function(){
|
||||
socket.decoded_token = decoded;
|
||||
socket.emit('authenticated');
|
||||
if (server.$emit) {
|
||||
server.$emit('authenticated', socket);
|
||||
} else {
|
||||
server.server.sockets.emit('authenticated', socket);
|
||||
}
|
||||
socket.decoded_token = decoded;
|
||||
socket.emit('authenticated');
|
||||
if (server.$emit) {
|
||||
server.$emit('authenticated', socket);
|
||||
} else {
|
||||
server.server.sockets.emit('authenticated', socket);
|
||||
}
|
||||
};
|
||||
|
||||
if(options.additional_auth){
|
||||
options.additional_auth(decoded, onSuccess, onError);
|
||||
}else{
|
||||
if(options.additional_auth && typeof options.additional_auth === 'function') {
|
||||
options.options.additional_auth(decoded, onSuccess, onError);
|
||||
} else {
|
||||
onSuccess();
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user