0
我想弄清楚如何在用户注册时哈希密码。我正在使用猫鼬和护照js。是否有任何节点模块可用于散列密码,这些密码可以用我当前的代码轻松实现?这里是我的LocalStrategies:如何在注册时使用我的猫鼬护照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);
}));
这里是我的用户模型:
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);
当我检查我的蒙戈DBS不散列密码。我如何散列它们?非常感谢!
的可能的复制(http://stackoverflow.com/questions/37668680/how-to-hash-password- [如何保存到数据库要与护照模块(护照本地)兼容之前哈希密码]之前节省转DB将要兼容,与护照模块) – matt