ldapjs - node.js synchronous function call for authentication -


i newbie in node.js, , trying learn how works. know default node.js function calls asynchronous. need ldap authentication in application need wait server response check whether user credentials right or wrong.the ldap part working fine not sure on how return data function call in synchronous way. below part of code.

router.js

var express = require('express'); var router = express.router(); var tools = require('./authenticateuser');  router.post('/authenticateuser', function(req, res) { // in below line calling method // should return userdn (a string)  tools.searchuser(req.body.user, req.body.passwd);  res.render('home.jade');  }); 

authenticateuser.js

module.exports = {     searchuser : function (username, password) {         admindn = *************;         adminpassword = '*********';         basedn = '***';           var ldap = require('ldapjs');         process.env.node_tls_reject_unauthorized = "0";         var adminclient = ldap.createclient({             url: '*******'          });         var opts = {             filter: '(&(objectclass=userproxyfull)(samaccountname=' + username + '))',             scope: 'sub',             attribute: ['samaccountname']          };           console.log('--- going try connect user ---');          try {             adminclient.bind(admindn, adminpassword, function (error) {                 if (error) {                     console.log(error.message);                     adminclient.unbind(function (error) {                         if (error) {                             console.log(error.message);                         } else {                             console.log('adminclient disconnected');                         }                     });                 } else {                      // searching client id in lds                      adminclient.search(basedn, opts, function (error, search) {                         console.log('searching.....' + userdn);                          search.on('searchentry', function (entry) {                             if (entry.object) {                                 // here need return object                                  //to router.js call in synchronous way                                  adminclient.unbind(function (error) {                                     if (error) {                                         console.log(error.message);                                     }                                 });                               }                         });                          search.on('error', function (error) {                             console.error('error: ' + error.message);                         });                       });                 }             });         } catch (error) {             console.log(error);             adminclient.unbind(function (error) {                 if (error) {                     console.log(error.message);                 } else {                     console.log('client disconnected');                 }             });         } {             adminclient.unbind(function (error) {                 if (error) {                     console.log(error.message);                 } else {                     console.log('client disconnected');                 }             });         }      },      }; 

you have pass res.render('home.jade') function(the callback) searchuser function.

it should

tools.searchuser(req.body.user,                  req.body.password,                   res} ) 

searchuser function

searchuser : function (username, password,res) {     ...     finally(){       res.render('home.jade');     } } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -