Auth0 Home Blog Docs

DNS lookups in rules

I’d like to lookup a DNS TXT record in a rule. The rules docs suggest the dns module is available, and I can require it without error.

However, when I try to call dns.resolveTxt my callback is never called.

Here’s a simple case that doesn’t work - can anyone spot what I’m doing wrong?

function (user, context, callback) {
  var dns = require('dns');
  
  var hostname = "google.com";
  console.log("hostname", hostname);
  dns.resolveTxt(hostname, function(err, result) {
    console.log("result", err, result, JSON.stringify(result));
  });

  return callback(null, user, context);
}

Ergh, this was just my javascript inexperience. Of course, the callback must be inside the dns lookup. Like this:

function (user, context, callback) {
  var dns = require('dns');
  
  var hostname = "google.com";
  console.log("hostname", hostname);
  dns.resolveTxt(hostname, function(err, result) {
    console.log("result", err, result, JSON.stringify(result));
    return callback(null, user, context);
  });
}
1 Like

Glad you were able to figure this one out and shared it with the rest of community!