如何在passport-local-mongoose中验证密码

时间:2017-04-27 12:33:36

标签: node.js mongodb passport.js passport-local

我使用护照-local-mongoose护照登记和登录用户。

这是我用来登录用户的代码:

print(ctree1)
Conditional Inference Tree 

392 samples
  8 predictor
  2 classes: 'neg', 'pos' 

No pre-processing
Resampling: Cross-Validated (10 fold, repeated 2 times) 
Summary of sample sizes: 691, 692, 691, 691, 691, 691, ... 
Resampling results across tuning parameters:

  mincriterion  Accuracy   Kappa    
  0.01          0.7349111  0.4044195
  0.50          0.7485731  0.4412557
  0.99          0.7323906  0.3921662

Accuracy was used to select the optimal model using  the largest value.
The final value used for the model was mincriterion = 0.5.

一切都很有趣但是user.validPassword。 我在用户模型中定义它:

passport.use(new localStrategy({ usernameField: 'email' }, function(email, password, done) {
  User.findOne({ email: email }, function(err, user) {
    if (err) { return done(err); }
    if (!user) {
      return done(null, false, { message: 'Incorrect username or password.' });
    }
    if (!user.validPassword(password)) {
      return done(null, false, { message: 'Incorrect username or password.' });
    }
    return done(null, user);
  });
}));

由于哈希密码将保存到数据库中,我不知道如何验证密码。

例如,这是一个保存在数据库{

中的用户
userSchema.methods.validPassword = function(password) {
    // What should I write here?
};

我也使用简单的本地身份验证。

非常感谢任何帮助。非常感谢。

3 个答案:

答案 0 :(得分:0)

我假设您有一种方法可以在保存之前生成salt和hash。

在您的validPassword方法中,您调用相同的方法并比较用户密码的结果,并将其与数据库中的结果进行比较/如果匹配,那么您就是好的。

作为替代方案,有管理这个的库。我使用bcrypt。请在此处查看示例:

http://xxxxxx.azurewebsites.net/Account/Oauth

答案 1 :(得分:0)

这是一个示例代码,请注意User.findOne()函数远非完整,它只是为了让您了解此代码相对于您的代码所处的位置,并让您了解应进行哪些更改被制成。

  passport.use('local.signup', new LocalStrategy({
   usernameField:'email', //it can be email or whatever one chooses
   passwordField:'password',
   confirmField:'password',
   passReqToCallback:true//here is the trick.u pass everything you want to do to a callback
   },function (req,email, password, done) {
      req.checkBody('email','Invalid e-mail address...').notEmpty().isEmail().normalizeEmail();//validate email
      req.checkBody('password','Invalid password...').notEmpty().isLength({min:8});//validate pass to be min 8 chars but you can provide it with checking for capital letters and so on and so forth
      var errors = req.validationErrors();
      if(errors){
      var messages = [];
      errors.forEach(function (error) {
      messages.push(error.msg)
     });
    return done(null, false, req.flash('error', messages))
    }

 User.findOne({ email: req.body.email }, function (err, user) {

// Make sure user doesn't already exist
   if (user) return done(null, false, {message:'The email address you have 
   entered is already associated with another account.'
  });

答案 2 :(得分:0)

如果您使用的是本地护照猫鼬,它将通过输入密码来创建盐并进行哈希运算。因此,在用户架构中不必保存密码字段。

但是护照本地猫鼬的本地策略与普通护照本地策略完全不同。就像

passport.use(new LocalStrategy(User.authenticate()));

这将检查输入的密码,并用盐和哈希值对其进行检查。

您编写的代码用于常规的本地策略。如果您使用的是护照本地猫鼬,那就不应该使用。

这是应该对护照本国猫鼬进行序列化和反序列化的方式:

 passport.serializeUser(User.serializeUser());passport.deserializeUser(User.deserializeUser());