2016-07-30 10 views
0

私が取り組んでいるこの個人的なプロジェクトのサーバー側部分の第一歩として、ログインサービスのプロトタイプを手に入れようとしています。それは3年前から出ている(マルク・Wandschneiderによって)学習のNode.jsブック、外にあるため、すでに動作するように証明されたように私は、コードherehereを参照しようとします。の機能に到達していません

私の実際のサーバコードは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

EDIT

私はsetTimeoutメソッドを含めるようにコードを変更しても、ユーザー機能でthisコンテキストを施行。あなたのgitリポジトリからダウンロードした自分のマシン上のすべてのコードを実行すると、ユーザーが認証されて、そこにないindex.htmlが検索されます。認証が機能しています!

コードは、塩と続行する前に終了するのを待って、ハッシュされていません。あなたがDBに書いていたら、これを "事前に"保存して約束を使用することができます。しかし、これはあなたのために今のところ回避策を与えるでしょう。私はのsetTimeoutを使用することが推奨されるアプローチが、問題が何であるかを証明する方法ではないことを追加する必要があります

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; 
} 

:ユーザーオブジェクト生成器内部の

self
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); 
    } 
} 

割り当てthis。理想的には、これはコールバック、約束、またはジェネレータを使用して行う必要があります。

+0

結果ではないことを、あなたのリンクを経由して、http://stackoverflow.com/help/mcve –

+0

実行しているノードのバージョンは?私はbashで動いているので、私はその環境に慣れていません。あなたのアプリがJQueryをインポートしようとしているようですね? – alexi2

+0

実行しているノードのバージョンが不明で、bashでも実行しています。はい、私のアプリはjQuery(クライアント側)をインポートしています。 –

関連する問題