0

I want to write a function that adds the params of an ldap server after checking if the binding with server is right otherwise the data won't get added in the db ; i have to use feathers js. I tried to write a couple of lines to validate the params before adding them to the db : const ldap=require("ldapjs"); but the problem is that when i do console.log(errors) inside and outside of ldap.bind function : i noticed that the result is only visible inside the function but not to the rest of the code ! I need it to be visible to the other parts as well so that I can use it , I want to know how to fix that.

this is the code and what the console.log has as a result.

module.exports = (ldapConnexion, mode = "create") => {

  const errors = {
    url_path: [],
    browsingAccount: [],
    password: [],
    userSearchBase:[],


  };

  const client=ldap.createClient({url:ldapConnexion.url_path})
 client.bind(ldapConnexion.browsingAccount,ldapConnexion.password,function(err){
    if (err){errors.browsingAccount.push("unvalid credentials:"+err);console.log(err)}})

console.log(errors)

const hasErrors = Object.keys(errors).reduce(
  (res, errorType) =>
    res || (errors[errorType] && errors[errorType].length > 0),
  false
);
return { errors, hasErrors };


    }
  • Hi -- did you mean to include what `console.log(errors)` prints out? It would be useful to help diagnose. –  Jun 15 '20 at 09:40
  • No , what i meant was : when i make console.log inside the code like this : const client=ldap.createClient({url:ldapConnexion.url_path}) client.bind(ldapConnexion.browsingAccount,ldapConnexion.password,function(err){ if (err){errors.browsingAccount.push("unvalid credentials:"+err);console.log(errors.browsingAccount)}}) i get this as result { 'unvalid credentials:InvalidCredentialsError: 80090308: LdapErr: DSID-0C0903C5, comment: AcceptSecurityContext error, data 52e, v23f0\u0000' ], } that means client.bind is returning an error – Maryem Anane Jun 15 '20 at 10:20
  • but when i w right console.log(errors) outside of the function this way : client.bind(ldapConnexion.browsingAccount,ldapConnexion.password,function(err){ if (err){errors.browsingAccount.push("unvalid credentials:"+err);}}) console.log(errors.browsingAccount) I get this as result : { browsingAccount: [] } it means that the result is only visible inside of the function client.bind but from the outside i can't see it . – Maryem Anane Jun 15 '20 at 10:22
  • I want to know how i can make it accessible out of the function as well so that i can use it in error handling – Maryem Anane Jun 15 '20 at 10:22

1 Answers1

0

to do that we can implement the code this way : if this can interrest someone in a different file do :

  const ldap = require('ldapjs');

module.exports = (ldapConnexion, mode = "create",cb) => {


  const errors = {
    url_path: [],
    browsingAccount: [],
    password: [],
    userSearchBase: [],


  };
  var opts ={
         
    rejectUnauthorized: false, // To force the client not to check self-signed certificates
}

//create the ldap client with the list of informations mentionned in the form   
var client = ldap.createClient({
    url:ldapConnexion.url_path, tlsOptions:opts
});
  client.bind(ldapConnexion.browsingAccount, ldapConnexion.password, function (err) {
    if (err) {
      errors.browsingAccount.push("unvalid credentials:" + err.message);
      //console.log(errors);
      cb(errors,true);
      client.unbind(function(err){if(err){console.log(err)}});

    } client.unbind(function(err){if(err){console.log(err)}});
  })

 // console.log(errors)


  if (!ldapConnexion.url_path) {
    errors.url_path.push("Url obligatoire");

  }
  if (!ldapConnexion.browsingAccount) {
    errors.browsingAccount.push("le compte lecteur est obligatoire");

  }
  if (!ldapConnexion.password) {
    errors.password.push("le mot de passe est obligatoire");

  }



  const hasErrors = Object.keys(errors).reduce(
    (res, errorType) =>
      res || (errors[errorType] && errors[errorType].length > 0),
    false
  );
  cb(errors, hasErrors)
}

in the hooks :

  const validateConnexionHook = function (mode = "create") {
  return context => {
    return new Promise((resolve, reject) => {
      const ldapConnexion = context.data || {};

      validateConnexion(ldapConnexion, mode, (errors, hasErrors) => {
        console.log('**************')
        // console.log(errors);
        console.log(hasErrors);
        if (hasErrors) {
          context.error = errors;
          reject(errors)
        }
        else setTimeout(function () { resolve(); }, 3000);
      })

    })
  }
}