0

I'm building a function that is checking a user password with linux password shadow file. The problem is that the main (sync) function is returning undefined before the inner async function is finish, and because of that it always return undefined.

How can I change the main function to async so it will wait for the async function to finish?

Please advice. Thnaks in advance

Adrian

  function checkPass(a, b) {
    "use strict";
    var fs = require('fs');
    var sha512crypt = require('sha512crypt-node');
    //
    var shadow = '/tmp/shadow';
    var userNameCheck = a;
    var passwordCheck = b;
    //
    var response;

    fs.readFile(shadow, function (err, logData) {
        if (err) throw err;

        // logData is a Buffer, convert to string.
        var shadowFile = logData.toString();
        var shadowList = shadowFile.split('\n');

        for (var line in shadowList) {
            var userEntree = shadowList[line].split(":");

            if (userEntree[0] === userNameCheck) {

                var username = userEntree[0];
                var fullShadow = userEntree[1];
                //
                var fullShadowSplit = userEntree[1].split('$');
                //
                var passwordAlgorithm = fullShadowSplit[1];
                var saltShadow = fullShadowSplit[2];
                var passwordShadow = fullShadowSplit[3];
                //

                var hash = sha512crypt.sha512crypt(passwordCheck, saltShadow)
                //console.log(hash + '===' + fullShadow);
                if (hash === fullShadow) {
                    console.log('LOG: oldPassCorrect');
                    //return 'oldPassCorrect';
                    response = 'oldPassCorrect';
                    return response;
                    break;
                } else {
                    console.log('LOG: oldPassIncorrect');
                    //return 'oldPassCorrect';
                    response = 'oldPassIncorrect';
                    return response;
                    break;
                }
            }
        }
    });
}


console.log("Function response: " + checkPass('share', 'qwerty'));
Adrian Gherasim
  • 135
  • 1
  • 4
  • 10

1 Answers1

0

I add a callback to the main function:

function checkPass(a, b, callback) {
    "use strict";
    var fs = require('fs');
    var sha512crypt = require('sha512crypt-node');
    //
    var shadow = '/tmp/shadow';
    var userNameCheck = a;
    var passwordCheck = b;
    //
    var response;

    fs.readFile(shadow, function (err, logData) {
        if (err) throw err;

        // logData is a Buffer, convert to string.
        var shadowFile = logData.toString();
        var shadowList = shadowFile.split('\n');

        for (var line in shadowList) {
            var userEntree = shadowList[line].split(":");

            if (userEntree[0] === userNameCheck) {

                var username = userEntree[0];
                var fullShadow = userEntree[1];
                //
                var fullShadowSplit = userEntree[1].split('$');
                //
                var passwordAlgorithm = fullShadowSplit[1];
                var saltShadow = fullShadowSplit[2];
                var passwordShadow = fullShadowSplit[3];
                //

                var hash = sha512crypt.sha512crypt(passwordCheck, saltShadow)
                //console.log(hash + '===' + fullShadow);
                if (hash === fullShadow) {
                    console.log('LOG: oldPassCorrect');
                    response = 'oldPassCorrect';
                    callback(response);
                    break;
                } else {
                    console.log('LOG: oldPassIncorrect');
                    response = 'oldPassIncorrect';
                    callback(response);
                    break;
                }
            }
        }
    });
}


checkPass('share', 'qwerty', function (response) {
        console.log("CallBack: " + response);
    });
Adrian Gherasim
  • 135
  • 1
  • 4
  • 10
  • This help me: http://stackoverflow.com/questions/18950984/return-value-from-asynchronous-function-in-nodejs – Adrian Gherasim Jun 09 '15 at 15:56
  • It's not only the right way to go, it's the **only** way to go. You can get a bit more fancy with promises but it's still callback based. – slebetman Jun 09 '15 at 16:00
  • Actually there's one thing you need to add. It's standard in node to have callbacks take two arguments, err and result. So instead of the line `if (err) throw err;` do `if (err) return callback(err);` And instead of doing `callback(response);` do `callback(null, response)`. – nickclaw Jun 10 '15 at 02:07
  • Thanks all for all the help – Adrian Gherasim Jun 10 '15 at 05:19