首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Passport和mysql创建用户

使用Passport和mysql创建用户
EN

Stack Overflow用户
提问于 2016-08-17 22:31:12
回答 1查看 651关注 0票数 1

我正在开发一个MySql,Express,Angular,NodeJS应用程序,我试图注册用户,但我找不到一个合适的来源来提供我想要的信息。

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

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

EN

回答 1

Stack Overflow用户

发布于 2016-08-17 22:50:43

我们使用命名策略,因为默认情况下,我们有一个用于登录的策略和一个用于注册的策略,如果没有名称,它将被称为'local‘。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38999517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档