{"action":"created","issue":{"url":"https://api.github.com/repos/socketio/socket.io/issues/1888","repository_url":"https://api.github.com/repos/socketio/socket.io","labels_url":"https://api.github.com/repos/socketio/socket.io/issues/1888/labels{/name}","comments_url":"https://api.github.com/repos/socketio/socket.io/issues/1888/comments","events_url":"https://api.github.com/repos/socketio/socket.io/issues/1888/events","html_url":"https://github.com/socketio/socket.io/issues/1888","id":49898752,"number":1888,"title":"Root nsp middleware always called before sub nsp middleware authorization ","user":{"login":"peteruithoven","id":523210,"avatar_url":"https://avatars.githubusercontent.com/u/523210?v=3","gravatar_id":"","url":"https://api.github.com/users/peteruithoven","html_url":"https://github.com/peteruithoven","followers_url":"https://api.github.com/users/peteruithoven/followers","following_url":"https://api.github.com/users/peteruithoven/following{/other_user}","gists_url":"https://api.github.com/users/peteruithoven/gists{/gist_id}","starred_url":"https://api.github.com/users/peteruithoven/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/peteruithoven/subscriptions","organizations_url":"https://api.github.com/users/peteruithoven/orgs","repos_url":"https://api.github.com/users/peteruithoven/repos","events_url":"https://api.github.com/users/peteruithoven/events{/privacy}","received_events_url":"https://api.github.com/users/peteruithoven/received_events","type":"User","site_admin":false},"labels":[{"id":516855872,"url":"https://api.github.com/repos/socketio/socket.io/labels/Closed%20due%20to%20inactivity","name":"Closed due to inactivity","color":"006b75","default":false},{"id":130878,"url":"https://api.github.com/repos/socketio/socket.io/labels/Namespaces","name":"Namespaces","color":"FFD700","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2014-11-24T15:04:46Z","updated_at":"2017-01-12T15:40:40Z","closed_at":null,"body":"It's now possible to define middleware per namespace, but when this is used for authorization there is an issue. The issue is that the root namespace authorization is always called first, when this fails the namespace specific authorization isn't called. This makes it \"impossible\" to send the client an error from the namespace it was connecting to.\nNot sure what would be the best solution here... \n\n``` javascript\nvar express = require('express');\nvar app = express();\nvar http = require('http').Server(app);\nvar io = require('socket.io')(http);\nvar socketClient = require('socket.io-client');\nvar debug = require('debug')('nspAuthorization');\nvar port = 5000;\n\n// SERVER\nhttp.listen(port, function(){\n  debug('server listening on *:'+port);\n});\nvar rootNSP = io.of('/');\nvar subNSP = io.of('/namespace');\nrootNSP.use(authorization); // when enabled, the user won't get a error\nsubNSP.use(authorization);\nfunction authorization(socket, next) {\n  debug('authorization: to nsp: ',socket.nsp.name);\n  var query = socket.handshake.query;\n  if (query.secret === 'abc') {\n    debug(\"  valid secret\");\n    next();\n  } else{\n    debug(\"  invalid secret\");\n    next(new Error('invalid secret'));\n  }\n}\n\n// CLIENT\nvar url = 'http://localhost:'+port;\ndebug('namespace: connecting');\nvar socket = socketClient.connect(url+'/namespace?secret=fake');\nsocket.on('error',function(err) { // not received when root authorization middleware is enabled\n  debug('namespace: error: ',arguments);\n});\nsocket.on('connection',function() { \n  debug('namespace: connected');\n});\n```\n"},"comment":{"url":"https://api.github.com/repos/socketio/socket.io/issues/comments/272196216","html_url":"https://github.com/socketio/socket.io/issues/1888#issuecomment-272196216","issue_url":"https://api.github.com/repos/socketio/socket.io/issues/1888","id":272196216,"user":{"login":"darrachequesne","id":13031701,"avatar_url":"https://avatars.githubusercontent.com/u/13031701?v=3","gravatar_id":"","url":"https://api.github.com/users/darrachequesne","html_url":"https://github.com/darrachequesne","followers_url":"https://api.github.com/users/darrachequesne/followers","following_url":"https://api.github.com/users/darrachequesne/following{/other_user}","gists_url":"https://api.github.com/users/darrachequesne/gists{/gist_id}","starred_url":"https://api.github.com/users/darrachequesne/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/darrachequesne/subscriptions","organizations_url":"https://api.github.com/users/darrachequesne/orgs","repos_url":"https://api.github.com/users/darrachequesne/repos","events_url":"https://api.github.com/users/darrachequesne/events{/privacy}","received_events_url":"https://api.github.com/users/darrachequesne/received_events","type":"User","site_admin":false},"created_at":"2017-01-12T15:40:40Z","updated_at":"2017-01-12T15:40:40Z","body":"That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a [fiddle](https://github.com/darrachequesne/socket.io-fiddle) reproducing the issue if possible)."}}
{"actor":{"display_login":"darrachequesne"}}