Funktionen werden nicht erreicht
Ich versuche, den Prototyp des Anmeldedienstes als ersten Schritt im serverseitigen Teil dieses persönlichen Projekts, an dem ich arbeite, in Betrieb zu nehmen. Ich versuche den Code zu referenzierenHie undHie, wie es aus dem Learning Node.js-Buch (von Marc Wandschneider) hervorgeht, das seit drei Jahren herauskommt und sich somit bereits bewährt hat.
Mein aktueller Servercode ist unter @ zu findedieser StackOverflow-Link. Ich überspringe den Datenbankteil, weil ich @ ha Probleme beim Aufstehen, und stattdessen mache ich mein./helpers/users.js
aussehen wie
exports.version = "0.0.1";
var async = require('async'),
bcrypt = require('bcrypt');
var helpers = require('./helpers.js');
function User(id, email, displayName, password, deleted)
{
this.userID = id;
this.email = email;
this.displayName = displayName;
if (User.connectedToDatabase) this._password = password;
else
{
bcrypt.genSalt(10, function (err, salt) {
// this, for some reason, isn't getting called. Literally, I never see "I'm here"
console.log("I'm here...");
bcrypt.hash(password, salt, function (err, hash) {
if (!err)
{
this._password = hash;
console.log("this._password == " + this._password);
}
else
{
console.log("Error occurred: ");
console.log(err);
}
})
});
}
//this._password = password;
this.deleted = deleted;
}
User.connectedToDatabase = false;
User.prototype.userID = 0;
User.prototype.email = null;
User.prototype.displayName = null;
User.prototype._password = null;
User.prototype.deleted = false;
User.prototype.checkPassword = function (password, callback)
{
bcrypt.compare(password, this._password, callback); // returns false
}
User.prototype.responseObject = function() {
return {
id: this.userID,
email: this.email,
displayName: this.displayName
};
}
exports.login = function (req, res) {
var dblessPrototype = true;
// get email address from req.body, trim it and lowercase it
var email = req.body.emailAddress ?
req.body.emailAddress.trim().toLowerCase() :
"";
// begin the login process
async.waterfall([
// prelimninary verification: make sure email,password are not empty, and that email is of valid format
function(cb)
{
// if no email address
if (!email)
{
// send error via cb
cb(helpers.missingData("email_address"));
}
// if '@' not found in email address
else if (email.indexOf('@') == -1)
{
// then email address is invalid
cb(helpers.invalidData("email_address"));
}
// if password is missing from req.body
else if (!req.body.password)
{
// tell next function about that
cb(helpers.missingData("password"));
}
// we are ready to move on otherwise
else cb(null);
},
// TODO: lookup by email address
// check the password
function (userData, cb)
{
var u;
if (dblessPrototype)
{
u = new User(0,
"[email protected]",
"SampleAdmin",
"Sample0x50617373");
}
else
{
u = new User(userData);
}
console.log("u._password == " + u._password);
console.log("req.body.password == " + req.body.password);
u.checkPassword(req.body.password, cb);
},
// time to set status of authenticiation
function (authOK, cb)
{
console.log("authOK == " + authOK);
if (!authOK)
{
cb(helpers.authFailed());
return;
}
// set status of authenticiation in req.session
req.session.loggedIn = true;
req.session.emailAddress = req.body.emailAddress;
req.session.loggedInTime = new Date();
}
],
function (err, results)
{
if (err)
{
console.log(JSON.stringify(err, null, '\t'));
if (err.code != "already_logged_in")
{
helpers.sendFailure(res, err);
console.log("Already logged in...");
}
}
else
{
helpers.sendSuccess(res, { loggedIn: true });
console.log("Log in successful...");
}
});
}
Bei der Überprüfung des Passworts,u._password
ist null (es wird nie gesetzt, was bedeutet, dass das asynchronebcrypt.genSalt()
code wird nicht aufgerufen. Auch weder die letzte Funktion im Array, die der erste Parameter von @ iasync.waterfall()
noch die Funktion, die der letzte Parameter von @ iasync.waterfall()
werden gerufen.
Warum werden diese Funktionen nicht aufgerufen, und was kann ich dagegen tun?
EDIT: Ich habe die asynchrone Kennwortverschlüsselung synchronisiert, indem ich @ ersetzt hab
bcrypt.genSalt(10, function (err, salt) {
// this, for some reason, isn't getting called. Literally, I never see "I'm here"
console.log("I'm here...");
bcrypt.hash(password, salt, function (err, hash) {
if (!err)
{
this._password = hash;
console.log("this._password == " + this._password);
}
else
{
console.log("Error occurred: ");
console.log(err);
}
})
});
mitthis._password = bcrypt.hashSync(password, bcrypt.genSaltSync(10));
Er gelangt zum Kennwortvergleichsteil, bleibt eine Weile hängen und wechselt dann zum nächsten (letzten) Element des Arrays, ohne etwas an die Konsole zu drucken. Es ist, als ob dieses Element übersprungen wurde.