2017-04-27 2 views
0

私はpassport-local-mongooseを使ってパスポートを登録ユーザーとログインユーザーに使用しています。passport-local-mongooseでパスワードを検証する方法

これは、私は、ユーザーがログインするために使用するコードです:

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); 
    }); 
})); 

すべてが楽しいけどuser.validPasswordに動作します。 私は、ユーザーモデルで定義する:ハッシュパスワードはデータベースに保存されます

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

ので、私はパスワードを検証する方法がわかりません。

例えば、これは、私は、単純なパスポート、ローカル認証を使用するデータベース{

"_id" : ObjectId("5901d55c30967512407cdcd0"), 
    "salt" : "7e76e1de50856c0a0e219c48609e0c86e8036bd4aa48e5161635f88dd19b695b", 
    "hash" : "eb78dcb7109454457b0709b6b49f322c69454930e3a6ca90621b1f38d4a068c46a34d0af8ba4f3cb3c3f9c8a30889f54028f182e1a038ab5d642e408117a93b34a519e594f62ea2209ef350f17d388e07476c021fdd66979e5752305b2f41571c830c4e03300cfbddce70443debee06d921900e1f7b9f024ea8d875553f01989e9267b01cc7e9da70f5ee39085527a55c8b6a9f5c7f21e07166797d5446322f52ec79c8e6bfd3e31d5f3953d86a13775da09f7ac3a5a85e5342cd35324bc66055030ca738fa657c50ec4368fe1fd4a26448b460996dc85ddebf90f92796f2b1adf9b905e0590efcadd8326053e354afcc144a155ca7ba1a0f1269cd2c5edec9ef4c643e5ca58310bf3283ed21bb94da6b22c113d19baf091f62bf1776fdcd4bca572cc114ec991d780c18524aad34988d0045f9a1d35e6cda4a6be337d7c8dce8256a240ecac9c7f4ac6c231a3c258f3660b5dd6daf4e67f878fbd9af9e52f9d36266828f564e6ac86f3aed98f7af0bb39017f6080e41cb49237bec6eae77253200750be14e53e79e3fc8d29a3a5cc774905e47bc8df6e5ae9f1b38d9ef738a7cc7890aba4bbea757c694df5faaeed2c692adcc9d8bb0242a5ced98c6a015f5b0b3b475b4a78767a1e9e3c6c9f1bc1be42a835f9e54de3ce223f6190ed457ea972ee4be6f506fd3995411d05247b7102c396c3a16b0d4c26664833d2224191cc", 
    "username" : "stve45", 
    "email" : "[email protected]", 
    "name" : "Steve", 
    "__v" : 0 
} 

に保存された一人のユーザです。

ありがとうございます、ありがとうございます。

答えて

0

保存する前に塩とハッシュを生成する方法があるとします。

validPasswordメソッドでは、同じメソッドを呼び出して、ユーザーのパスワードinoutの結果と比較し、DBにあるものと比較します。一致する場合は正常です。

代わりに、これを管理するライブラリがあります。私はbcryptを使用します。以下のサンプルをご覧ください:

adminSchema.pre('save', function(next) { 
    var user = this; 

    // only hash the password if it has been modified (or is new) 
    if (!user.isModified('password')) { 
     return next(); 
    } 

    // password changed so we need to hash it (generate a salt) 
    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { 
     if (err) { 
      return next(err); 
     }else{ 
      // hash the password using our new salt 
      bcrypt.hash(user.password, salt, function(err, hash) { 
       if (err) { return next(err); } 
       // override the cleartext password with the hashed one 
       user.password = hash; 
       next(); 
      }); 
     } 
    }); 
}); 


adminSchema.methods.comparePassword = function(password, cb) { 
    console.log('comparing password'); 
    bcrypt.compare(password, this.password, function(err, isMatch) { 
     cb(err, isMatch); 
    }); 
}; 
関連する問題