Node.js和Passport对象没有validPassword方法
我正在使用Node.js+Express+Passport创建一个简单的身份验证(本地) 到目前为止,当输入错误的用户名或密码时,用户会被重定向到错误页面 但是当用户输入正确的用户名和密码时,我会得到这个错误Node.js和Passport对象没有validPassword方法,node.js,mongodb,authentication,passport.js,Node.js,Mongodb,Authentication,Passport.js,我正在使用Node.js+Express+Passport创建一个简单的身份验证(本地) 到目前为止,当输入错误的用户名或密码时,用户会被重定向到错误页面 但是当用户输入正确的用户名和密码时,我会得到这个错误 node_modules\mongoose\lib\utils.js:435 throw err; ^ TypeError: Object { _id: 50b347decfd61ab9e9e6768f, username: 'saleh', password:
node_modules\mongoose\lib\utils.js:435
throw err;
^
TypeError: Object { _id: 50b347decfd61ab9e9e6768f,
username: 'saleh',
password: '123456' } has no method 'validPassword'
我不确定那里出了什么问题
app.js(我删除了不必要的代码):
现在在routes/login.js中
var mongoose = require('mongoose');
var db = mongoose.createConnection('localhost', 'authTest');
var authSchema = mongoose.Schema({
username: 'string',
password: 'string'
});
var User = db.model('users', authSchema);
exports.index = function(req, res){
User.find(function (err, list) {
res.render('login', { title: 'Usernames and Passwords', users: list,msg:""});
});
};
谢谢您的时间。您正在使用
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
但您尚未定义validPassword
方法。将其附加到您的架构:
var authSchema = mongoose.Schema({
username: 'string',
password: 'string'
});
authSchema.methods.validPassword = function( pwd ) {
// EXAMPLE CODE!
return ( this.password === pwd );
};
编辑您还错误地定义了架构。应该是:
var authSchema = mongoose.Schema({
username: String,
password: String
});
请注意,
username
和password
都应该是String
类型对象,而不是strings“String”
,如果您知道我的意思的话。:) 看起来您是从passportjs网站上复制了这个示例,Jared没有提到如何实现它
在passport js github页面上,他有另一个(更简单的)示例;他完全删除了validPassword方法(第18行):
这就是我的应用程序的基础(使用加密技术)。我也是个傻瓜,花了一整天的时间才弄明白这一点。我使用了Jared的另一个示例应用程序的历史记录和这里的人们提供的一些加密建议 首先,我制作了一个方法,生成一个salt(一个字符串化的大随机数),使用salt和用户密码创建一个散列(借助nodejs‘crypto’模块),最后在mongoose保存新帐户之前每次都存储salt和散列
//make hash
userSchema.pre('save', function(next) {
var user = this;
if(!user.isModified('password')) return next();
var rand = (Math.floor(Math.random() * 1000000000)).toString(36);
var hash = crypto.createHash('md5').update(user.password + rand).digest("hex");
user.password = hash;
user.salt = rand;
next();
});
对于验证,我只需获取输入的密码(登录时),并尝试使用salt再次创建相同的哈希。然后将存储的哈希值与新哈希值进行比较,并相应地返回true或false
// Password verification
userSchema.methods.validPassword = function(password) {
var testhash = crypto.createHash('md5').update(password + this.salt).digest("hex");
if(testhash === this.password) {
return true;
} else {
return false;
}
}
谢谢,我真的需要一个更简单的例子,我还联系了Jared以制作更好的文档:)如果可以的话,还有一个问题。。你能给我推荐一本书或任何资源来更好地学习Node.js吗?@MuhammadSaleh我不知道任何Node.js书。我通过不断的谷歌搜索学会了一切。从上的教程开始。然后就是谷歌。Stackoverflow也是一个很好的知识来源。我就是这么学的。不过这是护照。难道还不应该有对密码进行哈希运算和检查的功能吗?当你意识到这已经是一年前的事了。没关系:)谢谢你的贡献这个答案可能应该更新,因为使用md5和
Math.random
是不安全的。
//make hash
userSchema.pre('save', function(next) {
var user = this;
if(!user.isModified('password')) return next();
var rand = (Math.floor(Math.random() * 1000000000)).toString(36);
var hash = crypto.createHash('md5').update(user.password + rand).digest("hex");
user.password = hash;
user.salt = rand;
next();
});
// Password verification
userSchema.methods.validPassword = function(password) {
var testhash = crypto.createHash('md5').update(password + this.salt).digest("hex");
if(testhash === this.password) {
return true;
} else {
return false;
}
}