护照 bcrypt 抛出“不正确的论点”

Sco*_*ton 1 bcrypt passport-local passport.js

已经在这方面工作了一段时间,似乎无法弄清楚。浏览了帖子,我可以找到 & stackoverflow 帖子。似乎没有任何效果。记录的错误:

Users/username/Sites/dev/node_modules/bcrypt-nodejs/bCrypt.js:642
    throw "Incorrect arguments";
        ^
Incorrect arguments
Run Code Online (Sandbox Code Playgroud)

user.js 和passport.js 可以在这里看到。 http://pastebin.com/CEy6QBkP

错误来自此函数和 compareSync

var isValidPassword = function(user, password){
    return bCrypt.compareSync(password, user.password);
}
Run Code Online (Sandbox Code Playgroud)

与 req.body.password 相比,与哈希和盐有关。任何帮助将不胜感激。谢谢。

小智 5

您可以检查两个变量的类型以确保它们都是字符串。不久前遇到了同样的问题。下面的代码对我有用。希望这会有所帮助。

var mongoose = require('mongoose')
var bcrypt = require('bcrypt-nodejs')

var userSchema = mongoose.Schema({
    username: String,
    password: String,
    fullname : String,
    role: String,

})

userSchema.methods.generateHash = function(password){
  return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null)
}

userSchema.methods.validPassword = function(password){
  return bcrypt.compareSync(password, this.password)
}

module.exports = mongoose.model('User', userSchema)
Run Code Online (Sandbox Code Playgroud)