2016-07-30 53 views
0

我想获得登录服务的原型了,因为在此的个人项目我工作的服务器端部分的第一步。我试图引用代码herehere,因为它是从学习Node.js的书(由Marc Wandschneider),它已经流传了三年,并因此已经证明工作。功能没有达到

我实际的服务器代码可以在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); 
      } 
     }) 
    }); 

,挂起一段时间,然后到下一个(最后)数组的元素,而不打印任何内容到控制台。就好像这个元素被跳过了一样。

+1

请只发布相关的代码行。 – undefined

+0

完整性如何? (我应该摆脱我的客户端的东西?) –

+0

@Vohuman我删除了我的客户端代码。我应该删除更多?我希望运行此代码的人能够接受并快速自行运行。 –

回答

1

编辑下载完整的应用程序,并在家里与它瞎搞了。

我更改了代码以包含setTimeout方法,并在User函数中强制执行this上下文。在我自己的机器上运行我从git仓库下载的所有代码,我已经达到了用户身份验证的地步,应用程序查找不存在的index.html。所以验证正在工作!

代码不是等待盐和散列继续之前完成。如果你正在写一个数据库,你可以这样做'预'保存并使用一个承诺。但是现在这会给你一个解决方法。

function (cb, userData) 
    { 
     var u; 
     if (dblessPrototype) 
     { 
      var authOK; 
      u = new User(0, 
       "[email protected]", 
       "SampleAdmin", 
       "Sample0x50617373"); 
       setTimeout(function() { 
        console.log("u._password == " + u._password); 
        console.log("req.body.password == " + req.body.password); 
        console.log(u) 
        u.checkPassword(req.body.password, function(err, res) { 
        if (err) { 
         console.log(err) 
        } else { 
         console.log(res) 
         // USER AUTHENTICATED 
         cb(null, true) 
        } 
        return res; 
        }); 
       }, 1000) 
     } 
     else 
     { 
      u = new User(userData); 
    } 
} 

分配thisself用户对象发生器内:

function User(id, email, displayName, password, deleted) 
{ 
    this.userID = id; 
    this.email = email; 
    this.displayName = displayName; 
    var self = this 
    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) 
       { 
        self._password = hash; 
        console.log("this._password == " + self._password); 
       } 
       else 
       { 
        console.log("Error occurred: "); 
        console.log(err); 
       } 
      }) 
     }); 
    } 
    // this._password = password; 
    this.deleted = deleted; 
} 

我要补充一点,用setTimeout的不是推荐的方法,但表现出的问题是什么的一种方式。理想情况下,这应该使用回调,承诺或生成器完成。

+0

结果:http://imgur.com/a/ zagna –

+0

你正在运行什么版本的节点?我在bash中跑步,所以我不熟悉那个环境。它看起来像你的应用程序试图导入JQuery? – alexi2

+0

不确定我正在运行的节点的版本,而且我也在bash中运行。是的,我的应用正在导入jQuery(客户端)。 –