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'));