此功能处理用户提供的用户名和密码。什么对象将此信息存储在节点中以供程序访问?什么加载代码来提供此对象的值?
var bcrypt = require("bcrypt");
var mc = require('mongodb').MongoClient;
var playersCollection;
var start = function(req, res){
var playername = req.body.playername;
var password = req.body.password;
playersCollection.findOne({playername: playername}, function(err, player){
if (err || !player){
req.session.destroy(function(err) {
res.redirect("/?error=invalid playername or password");
});
return;
}
bcrypt.compare(password, player.password, function(err, authenticated){
if(authenticated){
req.session.player = player;
delete req.session.player._id;
res.redirect("/" + player.room);
} else {
req.session.destroy(function(err) {
res.redirect("/?error=invalid playername or password");
});
}
});
});
}
答案 0 :(得分:0)
我们在Drywall中执行此操作。 Here are the two main methods we use
...
userSchema.statics.encryptPassword = function(password, done) {
var bcrypt = require('bcrypt');
bcrypt.genSalt(10, function(err, salt) {
if (err) {
return done(err);
}
bcrypt.hash(password, salt, function(err, hash) {
done(err, hash);
});
});
};
userSchema.statics.validatePassword = function(password, hash, done) {
var bcrypt = require('bcrypt');
bcrypt.compare(password, hash, function(err, res) {
done(err, res);
});
};
...
希望使用我们的代码作为参考可以帮助您。