Passport js,使用Handlebars获取用户输入

时间:2016-06-13 16:17:59

标签: node.js express handlebars.js passport.js

我开始使用护照,把手和mysql学习node.js。

我想创建一个注册页面,该页面将包含电子邮件地址,密码,名字和姓氏。但是在护照上,它只需要用户名和密码。

passport.use('local-signup', new passportlocal.Strategy({
        // 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){
    connection.query("select * from userInfo where email = '"+email+"'", function(err, res){       
        if(err){
            console.error('error');
            // done(null, null);
            return done(null, false, { message: req.flash('error', 'Please fill your data correctly') });
            // return;
        }
        if (res.length) {
            console.log('user exist');
            return done(null, false, { message: req.flash('error', 'That email is already registered.') });
        }
        else{                
            // create the user
            var newUserMysql = new Object();
            newUserMysql.email = email;
            newUserMysql.password = password;
            newUserMysql.firstName = req.firstName;

            console.log(newUserMysql.firstName);

            var insertQuery = "INSERT INTO userInfo ( email, password, firstName ) values ('" + email +"','"+ password +"','"+ firstName +"')";

            connection.query(insertQuery,function(err,rows){
            newUserMysql.id = rows.insertId;

            // send mail with defined transport object
            app.locals.mailer.sendMail(mailOptions, function(error, info){
                if(error){
                    return console.log(error);
                }
                console.log('Message sent: ' + info.response);
            });

            return done(null, newUserMysql);
            }); 

        }
    })
})); 

以下是我的把手的代码

<div class="ui vertical stripe segment">
    <div class="ui text container">
        <form action="" method="post">

            {{#if message}}
            <div class="ui red message">{{message}}</div>
            {{/if}}

            <div class="content">
                <div class="field required">    
                    <label>Email Address</label>
                    <div class="ui fluid input">
                        <input type="text" name="email" placeholder="Email Address">
                    </div>
                </div>
            </div>  

            <p></p>

            <div class="content">
                <div class="field required">    
                    <label>Password</label>
                    <div class="ui fluid input">
                            <input type="password" name="password" placeholder="Password">
                    </div>
                </div>
            </div>  

            <p></p>

            <div class="content">
                <div class="field required">    
                    <label>Repeat Password</label>
                    <div class="ui fluid input">
                            <input type="password" name="repeatPassword" placeholder="Repeat Password">
                    </div>
                </div>
            </div>  

            <p></p>

            <div class="content">
                <div class="field required">    
                    <label>First Name</label>
                    <div class="ui fluid input">
                        <input type="text" name="firstName" placeholder="First Name">
                    </div>
                </div>
            </div>  

            <p></p>

            <div class="content">
                <div class="field required">    
                    <label>Last Name</label>
                    <div class="ui fluid input">
                        <input type="text" name="lastName" placeholder="Last Name">
                    </div>
                </div>
            </div>  

            <p></p>

            <div class="content">
                <div class="field required">    
                    <label>Phone Number</label>
                    <div class="ui fluid input">
                        <input type="text" name="phoneNum" placeholder="Phone Number">
                    </div>
                </div>
            </div>  

            <p></p>

            <button class="ui yellow button">Sign Up</button>

            <div class="ui horizontal divider">
                OR
            </div>

            Already have an account? Please <a href="/login" class="item">Login here!</a> 
        </form>
    </div>
</div>

我尝试了以下solution,但它对我不起作用。

我认为我从把手输入护照到护照的方式是错误的,但我无法弄明白。

谢谢。

1 个答案:

答案 0 :(得分:0)

在Jose的帮助下弄清楚,两个语句都应添加一个(req.body.firstName)

 else{                
        // create the user
        var newUserMysql = new Object();
        newUserMysql.email = email;
        newUserMysql.password = password;
        newUserMysql.firstName = req.body.firstName;

        console.log(newUserMysql.firstName);

        var insertQuery = "INSERT INTO userInfo ( email, password, firstName ) values ('" + email +"','"+ password +"','"+ req.body.firstName +"')";