我正在开发一个MySql,Express,Angular,NodeJS应用程序,我试图注册用户,但我找不到一个合适的来源来提供我想要的信息。
我知道如何使用用户名和密码创建用户记录,但只知道如何将数据库存储为文本,而不是散列/加盐。
我在想,我只需要知道如何在用户注册时创建一个哈希密码。然后找出如何将散列后的密码放入数据库中,最后当用户想要登录时,比较密码。但这一切都感觉有点抽象,我找不到任何关于护照的信息。
发布于 2016-08-17 22:50:43
我们使用命名策略,因为默认情况下,我们有一个用于登录的策略和一个用于注册的策略,如果没有名称,它将被称为'local‘。
passport.use('local-signup', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) {
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
connection.query("select * from users where email = '"+email+"'",function(err,rows){
console.log(rows);
console.log("above row object");
if (err)
return done(err);
if (rows.length) {
return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
} else {
// if there is no user with that email
// create the user
var newUserMysql = new Object();
newUserMysql.email = email;
newUserMysql.password = password; // use the generateHash function in our user model
var insertQuery = "INSERT INTO users ( email, password ) values ('" + email +"','"+ password +"')";
console.log(insertQuery);
connection.query(insertQuery,function(err,rows){
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
});
}
});
}));有关更多详细信息,请查看此https://gist.github.com/manjeshpv/84446e6aa5b3689e8b84
https://stackoverflow.com/questions/38999517
复制相似问题