使用Passport创建用户& MySQL的

时间:2016-08-17 14:31:12

标签: mysql passport.js

我正在开发一个MySql,Express,Angular,NodeJS应用程序,我正试图围绕注册用户,我找不到提供我正在寻找的信息的propper源。

我知道如何使用用户名和密码创建用户记录,但只知道如何将数据库存储为文本而不是哈希/盐渍。

我想我只需要知道如何在用户注册时创建哈希密码。然后弄清楚如何将哈希密码放在数据库中,最后当用户想要登录时比较密码。但这一切都有点抽象,我找不到任何有关护照的信息。

1 个答案:

答案 0 :(得分:0)

我们正在使用命名策略,因为我们有一个用于登录,一个用于注册  默认情况下,如果没有名称,它将被称为“本地”

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