3

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?

1 Answer 1

1

I solved this problem a few days ago for creating ldap-client every time in ldapBind function (in your case).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.