2015-04-02 96 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