我现在有
module.exports.comparePassword = function(candidatePassword, hash, callback) {
console.log(candidatePassword)
console.log(hash)
bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
if(err) throw err;
callback(null, ismatch);
});
}
现在的日志是
Server is up on port 3000
Connection has been established
Sat Jan 13 2018 14:45:36 GMT+0000 (GMT): GET /users/login
Sat Jan 13 2018 14:45:42 GMT+0000 (GMT): POST /users/login
testing1234
undefined
/Users/benbagley/Code/poetry-out-loud/models/user.js:101
if(err) throw err;
^
Error: Illegal arguments: string, undefined
at _async (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:286:46)
at Object.bcrypt.compare (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:304:13)
at Function.module.exports.comparePassword (/Users/benbagley/Code/poetry-out-loud/models/user.js:100:10)
at /Users/benbagley/Code/poetry-out-loud/routes/users.js:176:12
at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
at process._tickCallback (internal/process/next_tick.js:150:11)
这里似乎显示的是密码,但不是哈希。
原始
嗨,我收到以下错误,不知道是什么导致它
Message sent: <6434a712-dbb6-64f0-4b63-62f206f338c4@bens-mbp.lan>
Preview URL: https://ethereal.email/message/WlVWjq0qIgpSmhJbWloWhUGTHAp3fWC4AAAAbOQTYPu-4HjQWkI0i1uv5Ds
Sat Jan 13 2018 14:24:05 GMT+0000 (GMT): GET /users/login
Sat Jan 13 2018 14:24:24 GMT+0000 (GMT): POST /users/login
/Users/benbagley/Code/poetry-out-loud/models/user.js:99
if(err) throw err;
^
Error: Illegal arguments: string, undefined
at _async (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:286:46)
at Object.bcrypt.compare (/Users/benbagley/Code/poetry-out-loud/node_modules/bcryptjs/dist/bcrypt.js:304:13)
at Function.module.exports.comparePassword (/Users/benbagley/Code/poetry-out-loud/models/user.js:98:10)
at /Users/benbagley/Code/poetry-out-loud/routes/users.js:176:12
at model.Query.<anonymous> (/Users/benbagley/Code/poetry-out-loud/node_modules/mongoose/lib/model.js:4056:16)
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:273:21
at /Users/benbagley/Code/poetry-out-loud/node_modules/kareem/index.js:131:16
正在创建用户我无法登录。
以下是导致错误的行
module.exports.comparePassword = function(candidatePassword, hash, callback) {
bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
if(err) throw err;
callback(null, ismatch);
});
}
这是护照实施
passport.use(new LocalStrategy({
usernameField: 'email'
},
function(email, password, done) {
User.getUserByEmail(email, function(err, user){
if(err) throw err;
if(!user){
return done(null, false, {message: 'Unknown Email Address'});
}
User.comparePassword(password, user.password, function(err, ismatch){
if(err) throw err;
if(ismatch){
return done(null, user);
} else {
return done(null, false, {message: 'Invalid password'});
}
});
});
}));
答案 0 :(得分:1)
在我的情况下,错误是-
E:\web\Projects\webapp\auth.js:15
if(err) throw err;
^
Error: Illegal arguments: number, string
at _async (E:\web\Projects\webapp\node_modules\bcryptjs\dist\bcrypt.js:286:46)
at Object.bcrypt.compare (E:\web\Projects\webapp\node_modules\bcryptjs\dist\bcrypt.js:304:13)
at Promise (E:\web\Projects\webapp\auth.js:11:20)
at process._tickCallback (internal/process/next_tick.js:68:7)
[nodemon] app crashed - waiting for file changes before starting...
这是因为密码是数字,并且抛出错误!我刚刚将输入的密码转换为字符串!
解决错误之前的代码-
bcrypt.compare(password.toString(), user.password, (err, isMatch)=>{
if(err) throw err;
if(isMatch){
resolve(user);
}else{
//Password Wrong
reject("Auth Failed");
}
});
解决错误后-
我所做的是,我在password参数中添加了.toString()
函数以将其转换为字符串!
bcrypt.compare(password.toString(), user.password, (err, isMatch)=>{
if(err) throw err;
if(isMatch){
resolve(user);
}else{
//Password Wrong
reject("Auth Failed");
}
});
我希望它能对某人有所帮助。
答案 1 :(得分:0)
尝试console .log()
一些值,以确保一切都得到很好的定义。
module.exports.comparePassword = function(candidatePassword, hash, callback) {
console.log(candidatePassword)
console.log(hash)
bcrypt.compare(candidatePassword, hash, function(err, ismatch) {
if(err) throw err;
callback(null, ismatch);
});
}
答案 2 :(得分:0)
有同样的问题。
为传入的值添加console.log()表示它们是“未定义”。
我使用Postman测试了端点,以便将值添加到必填字段中,从而解决了问题。
结论:密码可能为空。使用console.log()检查它是否未定义。然后传递一些值给它。
答案 3 :(得分:0)
错误:非法参数:string,undefined可能意味着编译器无法识别您的哈希值或用户输入。首先尝试将输入内容与用户(从数据库返回的内容)一起记录,如果您的user.password
未返回字符串,则应检查Schema是否已预定义了字符串,并确保您不使用它select: false
用您的密码输入,它可能会限制查询。
答案 4 :(得分:0)
您可能已在密码上将select属性设置为false。这会导致错误。猫鼬不返回密码,因为您已将select设置为false。现在,您需要在查询时明确要求输入密码。 在要查询用户的位置使用select方法,并将+ password作为这样的字符串传递。
.select('+password')
答案 5 :(得分:0)
我过去使用MariaDB处理此问题,而罪魁祸首是大写的表列PASSWORD。当我将其更改为小写字母时,一切正常。
答案 6 :(得分:-1)
不要忘记使用toObject()
:
var pass = user.toObject().userPass;