title: Node JS LDAP Auth User
body: <p>I am creating a login authentication page, where a user would input there active directory username and password and using NodeJS I would check to see if it's valid, but I keep getting </p>
  
  <pre><code>[Error: LDAP Error Bad search filter]
  </code></pre>
  
  <p>or</p>
  
  <pre><code>[Error: Search returned != 1 results]
  </code></pre>
  
  <p>When I'm trying to search for the username and password, my code is below:</p>
  
  <p>I'm using: <a href="https://github.com/jeremycx/node-LDAP">https://github.com/jeremycx/node-LDAP</a>, let's say that the user entered a username of hhill</p>
  
  <pre><code>    var ldap = require('LDAP');
      var ldapServer = new ldap({ uri: 'ldap://batman.lan', version: 3});
  
      ldapServer.open(function(error) {
          if(error) {
             throw new Error('Cant not connect');
          } else {
              console.log('---- connected to ldap ----');
  
              username = '(cn='+username+')';
              ldapServer.findandbind({
                  base: 'ou=users,ou=compton,dc=batman,dc=lan',
                  filter: username,
                  password: password
              }, function(error, data) {
                  if(error){
                      console.log(error);
                  } else {
                      console.log('---- verified user ----');
                  }
              });
          }
      });
  </code></pre>
  
  <p>Does anyone have any suggestions on what I'm doing wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is the solution I came up with if anyone ever needs it, with the help of the answer below</p>
  
  <pre><code>    var username = request.param('username');
      var password = request.param('password');
  
      var ldap = require('ldapjs');
      ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B;
      var client = ldap.createClient({
            url: 'ldap://batman.com/cn='+username+', ou=users, ou=compton, dc=batman, dc=com',
            timeout: 5000,
            connectTimeout: 10000
      });
      var opts = {
        filter: '(&amp;(objectclass=user)(samaccountname='+username+'))',
        scope: 'sub',
        attributes: ['objectGUID']
      };
  
      console.log('--- going to try to connect user ---');
  
      try {
          client.bind(username, password, function (error) {
              if(error){
                  console.log(error.message);
                  client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
              } else {
                  console.log('connected');
                  client.search('ou=users, ou=compton, dc=batman, dc=com', opts, function(error, search) {
                      console.log('Searching.....');
  
                      search.on('searchEntry', function(entry) {
                          if(entry.object){
                              console.log('entry: %j ' + JSON.stringify(entry.object));
                          }
                      });
  
                      search.on('error', function(error) {
                          console.error('error: ' + error.message);
                      });
  
                      client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
                  });
              }
          });
      } catch(error){
          console.log(error);
          client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
      }
  </code></pre>