mirror of
https://github.com/Thream/socketio-jwt.git
synced 2024-07-21 09:38:31 +02:00
Merge branch 'master' of https://github.com/dbrugne/socketio-jwt into dbrugne-master
Conflicts: lib/index.js
This commit is contained in:
commit
84cc16cf49
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
|
.idea
|
||||||
node_modules/*
|
node_modules/*
|
22
lib/index.js
22
lib/index.js
@ -27,16 +27,26 @@ function noQsMethod(options) {
|
|||||||
if(options.required){
|
if(options.required){
|
||||||
clearTimeout(auth_timeout);
|
clearTimeout(auth_timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
jwt.verify(data.token, options.secret, options, function(err, decoded) {
|
jwt.verify(data.token, options.secret, options, function(err, decoded) {
|
||||||
var onError = function(){
|
// error handler
|
||||||
return socket.disconnect('unauthorized');
|
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) {
|
if (err) {
|
||||||
onError();
|
return onError(err, 'invalid_token');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// success handler
|
||||||
var onSuccess = function(){
|
var onSuccess = function(){
|
||||||
socket.decoded_token = decoded;
|
socket.decoded_token = decoded;
|
||||||
socket.emit('authenticated');
|
socket.emit('authenticated');
|
||||||
@ -47,8 +57,8 @@ function noQsMethod(options) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if(options.additional_auth){
|
if(options.additional_auth && typeof options.additional_auth === 'function') {
|
||||||
options.additional_auth(decoded, onSuccess, onError);
|
options.options.additional_auth(decoded, onSuccess, onError);
|
||||||
} else {
|
} else {
|
||||||
onSuccess();
|
onSuccess();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user