Passport js,使用 Handlebars 获取用户输入
Posted
技术标签:
【中文标题】Passport js,使用 Handlebars 获取用户输入【英文标题】:Passport js, get user input using Handlebars 【发布时间】:2016-10-14 03:49:50 【问题描述】:我开始使用护照、车把和 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,但它不适合我。
我认为我从车把获取输入数据到护照的方式是错误的,但我想不通。
谢谢。
【问题讨论】:
LocalStrategy 只接受回调中的用户名和密码字段,因为您可以访问请求对象,您可以像普通 HTTP 请求一样从请求正文 (req.body.firstName) 访问所需的属性。 对不起,我没有阅读您发布的另一个问题,但是首先查看您的代码是使用 req.body.firstName 来访问 firstName 变量。第二,你是如何发送请求的? 嗨@JoseHermosillaRodrigo,它通过添加请求正文(req.body.firstName)来工作。感谢您的帮助! 不客气!!您发布的答案也有一个很好的答案,其中提到了这一点! 【参考方案1】:在 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 +"')";
【讨论】:
以上是关于Passport js,使用 Handlebars 获取用户输入的主要内容,如果未能解决你的问题,请参考以下文章
Handlebars的使用方法文档整理(Handlebars.js)