I'm starting to learn node.js using passport, handlebars and mysql.
I want to create a signup page that will take email address, password, first name and last name. But on passport, it only takes the username and password.
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);
});
}
})
}));
and following is the code for my handlebars
<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>
I have tried the following solution, but it is not working for me.
I presume that my way of getting the input data from the handlebars to the passport is wrong, but I can't figure it out.
Thank you.