moscajs / mosca

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

lost user object in published event #586

Closed TangMonk closed 7 years ago

TangMonk commented 7 years ago

here is authenticate method:

const authenticate = function (client, username, password, callback) {
  debug(`login with username: ${username}, password: ${password}`)
  console.log(username, password);
  if (!username || !password) {
    return callback(null, false);
  }
  Car.findOne({ where: { car_login_name: username, car_password: md5(password) } }).then((car) => {
    if (!car) {
      callback(null, false);
    } else {
      client.user = car;
      callback(null, true);
    }
  })
}

server.authenticate = authenticate

here is published and clientConnected method:

server.on('published', function (packet, client) {
  debug(`published: username: ${client.user}, topic: ${packet.topic}`)
});

server.on('clientConnected', function (client) {
  debug(`connected: username: ${client.use}`)
});

The user will log to console in clientConnected, but cannot lot in published

mcollina commented 7 years ago

there are internal messages published by the broker (stats, etc) for which there is no client.