I have a simple module with a couple of functions to interact with an Active Directory.
Using the standard callback style, the code works just fine:
/**
* mylib.js
**/
const ActiveDirectory = require('activedirectory');
// Instantiate AD client
const ad = new ActiveDirectory({
url: 'ldaps://...',
baseDN: 'DC=...',
});
module.exports = {
// Authenticate user against the AD
authenticate: (username, password, callback) => {
// Authentication request
ad.authenticate(username, password, (err, status) => {
callback(err, status);
});
},
};
/**
* client.js
**/
const mylib = require('./mylib');
mylib.authenticate('<username>', '<password>', (err, status) => {
if (err) {
console.log(`Error: ${err}`);
return;
}
console.log(`Success: ${status}`);
});
Execution result:
> node client.js
Success: true
The next step was to refactor my lib method to use Promises instead of callbacks:
/**
* mylib.js
**/
const ActiveDirectory = require('activedirectory');
const Promise = require('bluebird');
//...
module.exports = {
// Authenticate user against AD
authenticate: Promise.method((username, password) => {
ad.authenticate(username, password, (err, status) => {
if (err) throw err;
return status;
});
}),
};
/**
* client.js
**/
const mylib = require('./mylib');
myLib.authenticate('<username>', '<password>').then((status) => {
console.log(`Success: ${status}`);
}).catch((err) => {
console.log(`Error: ${err}`);
});
Execution result:
> node client.js
Success: undefined
So it looks like the status
is not being resolved.
If I change the AD server URL to something else (to force a connection error and see the rejection) I can see both the resolve and the rejection log:
> node client.js
Success: undefined
/Users/.../mylib.js:84
if (err) throw err;
^
Error: connect ECONNREFUSED <IP>
at Object.exports._errnoException (util.js:1036:11)
at exports._exceptionWithHostPort (util.js:1059:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
What am I doing wrong? Can't figure it out.
return ad.authenticate()
If this does not work, wrap this method in another promise and return it. If you check the docs you'll see it returns a promise when doing async.