2015-04-02 73 views
0
Server running at http://localhost:3000/ 

/home/sankar/Cloud/config/strategies/local.js:26 
     if (!(user.authenticate(password))) 
       ^
TypeError: Object { firstName: 'raju', 
lastName: 'raju', 
email: '[email protected]', 
username: 'raju', 
password: 'raju', 
_id: 551b7ca79177c00c1342a3ee, 
__v: 0 } has no method 'authenticate' 
at Promise.<anonymous>     (/home/sankar/Cloud/config/strategies/local.js:26:15) 
    at Promise.<anonymous>  (/home/sankar/Cloud/node_modules/mongoose/node_modules/mpromise/lib/promis  e.js:177:8) 
    at Promise.EventEmitter.emit (events.js:95:17) 
    at Promise.emit (/home/sankar/Cloud/node_modules/mongoose/node_modules/mpromise/lib/promis  e.js:84:38) 
    at Promise.fulfill (/home/sankar/Cloud/node_modules/mongoose/node_modules/mpromise/lib/promis  e.js:97:20) 
    at /home/sankar/Cloud/node_modules/mongoose/lib/query.js:1400:13 
    at model.Document.init (/home /sankar/Cloud/node_modules/mongoose/lib/document.js:254:11) 
    at completeOne (/home/sankar/Cloud/node_modules/mongoose/lib/query.js:1398:10) 
    at Object.cb (/home/sankar/Cloud/node_modules/mongoose/lib/query.js:1155:11) 
    at Object._onImmediate (/home/sankar/Cloud/node_modules/mongoose/node_modules/mquery/lib/utils.js  :137:16) 

我得到只有當我試圖用正確的細節,以簽到這個錯誤是指不正確的用戶名被明確拋出錯誤(登錄)的密碼,但不爲正確的信譽。 它對SignUp工作正常。錯誤而驗證使用護照本地模塊節點JS

Local.js代碼:

var passport = require('passport'), 
    LocalStrategy = require('passport-local').Strategy, 
    User = require('mongoose').model('User'); 

module.exports = function() 
{ 
passport.use(new LocalStrategy(function(username, password, done) 
{ 
    User.findOne(
    { 
     username: username 
    }, 
    function(err, user) 
    { 
     if (err) 
     { 
      return done(err); 
     } 
     if (!user) 
     { 
      return done(null, false, 
      { 
       message: 'Unknown user' 
      }); 
     } 
     if (!(user.authenticate(password))) 
     { 
      return done(null, false, 
      { 
       message: 'Invalid password' 
      }); 
     } 
     return done(null, user); 
    }); 
    })); 
    } 

在我user.js的我已經添加了身份驗證方法。

UserSchema.methods.hashPassword = function(password) { 
    return crypto.pbkdf2Sync(password, this.salt, 10000, 
    64).toString('base64'); 
    }; 
    UserSchema.methods.authenticate = function(password) { 
    return this.password === this.hashPassword(password); 
    }; 
+0

不應該比較user.password和密碼嗎?我不知道認證應該做什麼,但用'if(user.password!== password)'替換上面的調用應該可以工作。 – rivarolle 2015-04-02 14:20:38

+0

是的,我做了我的user.js文件中的比較。我已經添加了上面的代碼,我如何比較密碼 – Reddy 2015-04-02 15:21:25

+0

可能重複[Mongoose實例方法未定義](http://stackoverflow.com/questions/10724876/mongoose-instance-method-is-undefined) – rivarolle 2015-04-02 19:04:09

回答

0

您尚未定義驗證方法。將它附加到您的模式:

var user= mongoose.Schema({ 
    username: 'string', 
    password: 'string' 
}); 
authSchema.methods.authenticate = function(pwd) { 
    return (this.password === pwd); 
}; 
+0

感謝您的答覆和對不起,我忘了添加驗證方法。現在我添加了已經存在於我的user.js文件中的authenticate方法。 – Reddy 2015-04-02 15:15:07