I'm new to node and I tried to make a basic app with authentification . Data are stored on a mongoDB remote server. My HTML form POST data to my server URL. Here the route :
app.post('/auth', function(req, res){
handleRequest(req, res);
});
And the called handler :
function handleRequest(request, response) {
if (request.method == 'POST') {
console.log("Trying to get POST");
var body = '';
request.on('data', function (data) {
body += data;
});
// Get datas, parse them and create user with it
request.on('end', function () {
var data = JSON.parse(body);
var login = data.login;
var password = data.password;
var email = data.email;
myUser = userClass.create(login,email,password);
console.log ("email : "+email);
console.log ("password : "+password);
// authenticate with user
var auth = userClass.authenticate(myUser,function(result){
console.log("result = "+result);
});
});
}
}
The userClass.authenticate :
exports.authenticate = function(user,callback){
var result = "false";
var query = User.where(
{
email : user.email,
password : user.password
});
query.findOne(function(err,user){
if(err){return handleError(err);}
if(user){
result = "true";
}
console.log(user);
});
console.log("callback inc")
callback(result);
}
I'm pretty sure it's not optimized but it's not what I'm looking for.
When I launch the server and I send it some POST (correct) data, this strange thing happens :

My user stored in remote DB is found , so in userClass.authenticate result = true
But when the callback function is ran, the log say it's false. Did I do a something wrong in the callback ?