moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

Fix 'client.user' undefined when authorizeSubscribe and authorizePublish #775

Closed danclive closed 5 years ago

danclive commented 5 years ago

I have followed this guide,Because I wanted to verify the user's credentials at the time the user subscribed and published, but I couldn't get the user name.

// In this case the client authorized as alice can publish to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
var authorizePublish = function(client, topic, payload, callback) {
  console.log(client.user) // undefined 
  callback(null, client.user == topic.split('/')[1]);
}

// In this case the client authorized as alice can subscribe to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
var authorizeSubscribe = function(client, topic, callback) {
  console.log(client.user) // undefined
  callback(null, client.user == topic.split('/')[1]);
}

So I changed the code so that I could get the user name.