0

I have a question over how to use auth in a bridge. Here we can see the next:

bridge.before('connect', function(ctx, next) {
if(ctx.auth.password !== '1234') {
ctx.badCredentials = true;
}
next();
});

On this example, I can access to ctx and check auth but, how can I include the auth from a client? In my client I do login in loopback with a user and get accessToken but I don't know how authenticate the client for publish in a channel.

Thank!

Sergio
  • 1

2 Answers2

1

Hooks allow you to run arbitrary functions before an action is performed (eg. publish, subscribe). For example, this code shows how to add authorization before a client’s message is published to a broker. This approach can be used with other actions as well (eg. subscribe). For more detail, check out the blog post on Pubsub here

// On the server...
// supported actions: connect, publish, and subscribe
bridge.before('publish', function(client, next) {
  var user = parseCredentials(client.credentials).user;
  canUserPublishToTopic(user, client.topic, function(err) {
    if(err) {
      // Not authorized, the action will be denied
      next(err); // Express-style error handling
    } else {
      next(); // the action will continue
    }
  });
});

// On the client...
client.publish('hello world');
JSimonsen
  • 2,642
  • 1
  • 13
  • 13
  • Thank, but how do add I authentication on my class client? On the server is as we see in the example but I can't find any example of how to do it on the client. – Sergio Jul 20 '15 at 09:23
0

I've just figured it out

You can pass your user credential through client options like this

var client = new Client({port: 3002, host: 'localhost', mqtt: {
    username: 'abc',
    password: 'abc'
  }}, Adapter, transport)