私が取り組んでいるこの個人的なプロジェクトのサーバー側部分の第一歩として、ログインサービスのプロトタイプを手に入れようとしています。それは3年前から出ている(マルク・Wandschneiderによって)学習のNode.jsブック、外にあるため、すでに動作するように証明されたように私は、コードhereとhereを参照しようとします。の機能に到達していません
私の実際のサーバコードはthis StackOverflow linkで発見することができます。私はtrouble getting it upを持っており、代わりに、私は同じように私./helpers/users.js
表情を作るので、私のデータベースの部分をスキップ:パスワードのチェックでは
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...");
}
});
}
、u._password
がヌルである(それは設定れることは決してありません、非同期bcrypt.genSalt()
コードすることを意味していますが呼び出されていませんまた、async.waterfall()
の最初のパラメーターでも、最後のパラメーターであるasync.waterfall()
の最後の関数も呼び出されません。
これらの関数が呼び出されないのはなぜですか?何ができるのですか?
編集:私はそれがパスワード比較部と、しばらくの間、ハングした後、(最後の)次に取得this._password = bcrypt.hashSync(password, bcrypt.genSaltSync(10));
で
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);
}
})
});
を交換することにより、非同期のパスワード暗号化同期を作りました要素をコンソールに何も印刷せずに配列の要素に追加します。あたかもその要素がスキップされたかのようです。完全なアプリをダウンロードし、自宅でそれをいじり後
関連するコード行のみを投稿してください。 – undefined
完全性はどうですか? (私はクライアント側のものを取り除くべきですか?) –
@Vohumanクライアント側のコードを削除しました。もっと削除する必要がありますか?私はこのコードを実行している人がそれを受け取り、すぐにそれを実行できるようにしたいと思います。その質問への答えは「いいえ」 –