Node.js passport本地策略在邮递员中工作,但不在浏览器中工作
我正在尝试使用电子邮件和密码设置身份验证。下面是signup.ejs中的部分代码:Node.js passport本地策略在邮递员中工作,但不在浏览器中工作,node.js,express,passport.js,passport-local,Node.js,Express,Passport.js,Passport Local,我正在尝试使用电子邮件和密码设置身份验证。下面是signup.ejs中的部分代码: <% if (message.length > 0) { %> <div class="alert alert-danger"><%= message %></div> <% } %> <!-- LOGIN FORM --> <form action="/signup" method="p
<% if (message.length > 0) { %>
<div class="alert alert-danger"><%= message %></div>
<% } %>
<!-- LOGIN FORM -->
<form action="/signup" method="post">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>
这是我正在使用的本地护照策略:
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) {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
// 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
User.findOne({ 'local.email': email }, function(err, user) {
// if there are any errors, return the error
if (err) return done(err)
// check to see if theres already a user with that email
if (user) {
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 newUser = new User()
// set the user's local credentials
newUser.local.email = email
newUser.local.password = newUser.generateHash(password)
// save the user
newUser.save(function(err) {
if (err) throw err
return done(null, newUser)
})
}
})
})
}
)
)
这里有一个指向我的链接,以获取完整的代码
我遇到的问题是,当我在请求正文中使用电子邮件和密码向邮递员发出post请求时,结果很好,我成功地重定向到配置文件路径。但是,当我试图通过填写页面上的表单登录时,我会被重定向回“/signup”路径。有人能帮我解决这个问题吗?我找到了答案。原因是表单没有将电子邮件和密码值传递给req.body。我将app.usebodyParser.json更改为app.usebodyParser.urlencoded{extended:true},它开始工作。一般提示:检查并查看是否调用了策略的验证处理程序。检查并查看电子邮件和密码是否包含预期值。检查并查看是否出现任何错误。
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) {
// asynchronous
// User.findOne wont fire unless data is sent back
process.nextTick(function() {
// 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
User.findOne({ 'local.email': email }, function(err, user) {
// if there are any errors, return the error
if (err) return done(err)
// check to see if theres already a user with that email
if (user) {
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 newUser = new User()
// set the user's local credentials
newUser.local.email = email
newUser.local.password = newUser.generateHash(password)
// save the user
newUser.save(function(err) {
if (err) throw err
return done(null, newUser)
})
}
})
})
}
)
)