Reputation: 175
I have created an authentication service using the following code in Node.js and ldapjs.
var when = require ('when');
var AuthenticationError = require('../errors/AuthenticationError');
var SessionManagerService = require('./SessionManagerService');
var ldap = require('ldapjs');
var client = ldap.createClient({
url: 'ldaps://ad.mycompany.com:636',
tlsOptions: {'rejectUnauthorized': false}
});
module.exports = {
signIn: function (email, password) {
return this.ldapBind(email, password).then(
function () {
return SessionManagerService.createSessionHash({email: email});
}
);
},
ldapBind: function (email, password) {
var deferred = when.defer();
client.bind(email, password, function(err) {
if (err) {
deferred.reject (new AuthenticationError('Invalid username and/or password!', 'Authentication.signIn.error'));
} else {
client.unbind();
deferred.resolve(true);
}
});
return deferred.promise;
},
};
When I authenticate my user for the first time, it works perfectly but it fails starting from the second attempt. The error message is: 'write after end'.
When I restart the node server it works again for the first attempt.
It looks like something is hanging but I don't know what. Any idea?
Upvotes: 3
Views: 1187
Reputation: 600
I solved this problem a few days ago for creating ldap-client every time in ldapBind function (in your case).
Upvotes: 1