Shibankar
Shibankar

Reputation: 846

node.js synchronous function call for authentication

i am completely newbie in node.js, and trying learn how it actually works. I know that by default all node.js function calls are asynchronous. Now i need LDAP authentication in my application where i need wait for the server response to check whether the user credentials are right or wrong.The ldap part is working fine but i am not sure on how to return data from a function call in synchronous way. below is the part of my code.

router.js

var express = require('express');
var router = express.Router();
var tools = require('./authenticateUser');

router.post('/authenticateUser', function(req, res) {
// In the below line i am calling the method which
// should return the 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 to try to 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 i need to return the object back 
                                //to the router.js from where i call in a 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');
                }
            });
        } finally {
            adminClient.unbind(function (error) {
                if (error) {
                    console.log(error.message);
                } else {
                    console.log('client disconnected');
                }
            });
        }

    },





};

Upvotes: 0

Views: 1198

Answers (1)

QoP
QoP

Reputation: 28397

You have to pass res.render('home.jade') as a function(the callback) to your searchUser function.

It should look like

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

searchUser function

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

Upvotes: 1

Related Questions