title: Getting "bad username or password" error while attempting to connect to broker
body: <p>I have followed the tutorial presented <a href="https://auth0.com/docs/tutorials/authenticating-devices-using-mqtt" rel="nofollow noreferrer">here</a>, but I couldn't connect a client to the server. I always get the following error message (full stacktrace):</p>
  
  <pre><code>Error: Connection refused: Bad username or password
  at MqttClient._handleConnack (${project_dir}/node_modules/mqtt/lib/client.js:760:24)
  at MqttClient._handlePacket (${project_dir}/node_modules/mqtt/lib/client.js:300:12)
  at process (${project_dir}/node_modules/mqtt/lib/client.js:242:12)
  at Writable.writable._write (${project_dir}/node_modules/mqtt/lib/client.js:252:5)
  at doWrite (${project_dir}/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:345:64)
  at writeOrBuffer (${project_dir}/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:334:5)
  at Writable.write (${project_dir}/node_modules/mqtt/node_modules/readable-stream/lib/_stream_writable.js:271:11)
  at Socket.ondata (_stream_readable.js:528:20)
  at emitOne (events.js:77:13)
  at Socket.emit (events.js:169:7)
  </code></pre>
  
  <p>I have double-checked my environment variables, whose values came from my Auth0 account, (particularly <code>CLIENT_ID</code>, <code>DOMAIN</code>, <code>CLIENT_SECRET</code> and <code>CONNECTION</code>) but they seem fine.</p>
  
  <p>I've changed the client's code a bit to match the current version of MQTT.js. Here is the code:</p>
  
  <pre><code>const mqtt = require('mqtt');
  const settings = {
      port: 1883,
      keepalive: 1000,
      protocolId: 'MQIsdp', // already tried 'MQTT' with protocol version 4
      protocolVersion: 3,
      clientId: 'a random id',
      username: 'an account at Auth0',
      password: 'the password of the account'
  }
  
  var client = mqtt.connect('mqtt://localhost', settings);
  
  client.on("connect", function () {
      client.subscribe("topic");
      console.log("connected");
  });
  
  client.on("message", function (topic, message) {
      // message is Buffer 
      console.log(message.toString());
      client.end();
  });
  </code></pre>
  
  <p>The broker code is very similar to the presented in the tutorial. I want to solve this error before changing it.</p>
  
  <pre><code>const mosca = require('mosca')
  const Auth0Mosca = require('auth0mosca');
  require('dotenv').config();
  
  const settings = {
      port: 1883
  };
  
  if (!process.env.AUTH0_DOMAIN || !process.env.AUTH0_CLIENT_ID ||
      !process.env.AUTH0_CLIENT_SECRET || !process.env.AUTH0_CONNECTION) {
      throw 'Make sure you have AUTH0_DOMAIN, AUTH0_CLIENT_ID, AUTH0_CLIENT_SECRET and AUTH0_CONNECTION in your .env file';
  }
  
  const auth0 = new Auth0Mosca('https://' + process.env.AUTH0_DOMAIN, process.env.AUTH0_CLIENT_ID, process.env.AUTH0_CLIENT_SECRET, process.env.AUTH0_CONNECTION);
  
  // mosca server
  const server = new mosca.Server(settings);
  
  server.authenticate = auth0.authenticateWithCredentials();
  server.authorizePublish = auth0.authorizePublish();
  server.authorizeSubscribe = auth0.authorizeSubscribe();
  
  server.on('ready', setup);
  
  // MQTT server is ready
  function setup() {
      console.log('Mosca server is up and running');
  }
  
  server.on('clientConnected', function(client) {
      console.log('New connection: ', client.id);
  });
  </code></pre>
  
  <p>I know that it is probably a stupid mistake or a library update that is causing this. For the latter case, here are the versions: </p>
  
  <pre><code>"auth0mosca": "^0.1.0",
  "mosca": "^2.3.0",
  "mqtt": "^2.5.0"
  </code></pre>
  
  <p>Finally, I have checked that the request reaches the broker.</p>