0
私はユーザー登録時にパスワードをハッシュする方法を理解しようとしています。私はマングースとパスポートを使用しています。現在のコードで簡単に実装できるパスワードをハッシュするために使用できるノードモジュールはありますか?どのように私はmongooseパスポートjsパスワードを登録時にハッシュしますか?
// Passport login LocalStrategy
passport.use('login', new LocalStrategy({
passReqToCallback : true
}, function(req, username, password, done) {
// check in mongo if a user with username exists or not
User.findOne({ 'username' : username },
function(err, user) {
// In case of any error, return using the done method
if (err)
return done(err);
// Username does not exist, log error & redirect back
if (!user){
console.log('User Not Found with username '+username);
return done(null, false,
req.flash('message', 'User Not found.'));
}
// User exists but wrong password, log the error
if (!user.validPassword(password)){
console.log('Invalid Password');
return done(null, false,
req.flash('message', 'Invalid Password'));
}
// User and password both match, return user from
// done method which will be treated like success
return done(null, user);
}
);
}));
passport.use('signup', new LocalStrategy({
passReqToCallback : true
},
function(req, username, password, done) {
findOrCreateUser = function(){
// find a user in Mongo with provided username
User.findOne({'username':username},function(err, user) {
// In case of any error return
if (err){
console.log('Error in SignUp: '+err);
return done(err);
}
// already exists
if (user) {
console.log('User already exists');
return done(null, false,
req.flash('message','User Already Exists'));
} else {
// if there is no user with that email
// create the user
var newUser = new User();
// set the user's local credentials
newUser.username = username;
newUser.password = password;
newUser.email = req.param('email');
// save the user
newUser.save(function(err) {
if (err){
console.log('Error in Saving user: '+err);
throw err;
}
console.log('User Registration succesful');
return done(null, newUser);
});
}
});
};
process.nextTick(findOrCreateUser);
}));
そして、ここに私のUserモデルは次のとおりです:ここに私のLocalStrategiesある私は私のモンゴDBSはパスワードはハッシュ化されていないチェック
var mongoose = require("mongoose");
var UserSchema = new mongoose.Schema({
username: String,
email: String,
password: String,
friends: [this]
});
UserSchema.methods.validPassword = function (pwd) {
return (this.password === pwd);
}
module.exports = mongoose.model("User", UserSchema);
。どのようにそれらをハッシュするのですか?本当にありがとう!
可能な重複(http://stackoverflow.com/questions/37668680/how-to-hash-password- [パスポートモジュール(パスポートローカル)と互換性があるように、DBに保存する前にパスワードをハッシュする方法] db-to-be-compatible-with-passport-module)を使用することができます。 – matt