我越來越開始與節點和密碼,我想稍微修改護照的默認驗證。 除了通常的用戶名/電子郵件和密碼字段之外,我想爲身份驗證添加一個暱稱字段。這就是我如何修改:的NodeJS,PassportJS:添加另一個領域的認證
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
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 {
/////////////// MODIFIED PART ////////////////
User.findOne({ 'local.nickname' : req.body.nickname }, function(err, user) {
if (user) {
return done(null, false, req.flash('signupMessage', 'That nickname is already taken.'));
/////////////////////////////////////////////
}
});
...
...
但該計劃與退出:退出代碼爲8
完成
throw new Error('Can\'t set headers after they are sent.'); ^POST /signup Error: Can't set headers after they are sent. 302 at ServerResponse.OutgoingMessage.setHeader (http.js:691:11) 72ms - 72b at ServerResponse.res.setHeader (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/express/node_modules/connect/lib/patch.js:63:22)
at ServerResponse.res.set.res.header (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/express/lib/response.js:527:10)
at ServerResponse.res.location (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/express/lib/response.js:657:8)
at ServerResponse.res.redirect (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/express/lib/response.js:698:8)
at allFailed (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/passport/lib/passport/middleware/authenticate.js:124:20)
at attempt (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/passport/lib/passport/middleware/authenticate.js:231:28)
at Context.delegate.fail (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/passport/lib/passport/middleware/authenticate.js:226:9)
at Context.actions.fail (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/passport/lib/passport/context/http/actions.js:35:22)
at verified (/home/madu/Programming/NodeJS/easy-node-authentication-local/node_modules/passport-local/lib/passport-local/strategy.js:82:30)
過程我有兩個問題:
- 如何解決這個問題 -
- 這是做這件事的理想方式(用戶名後用暱稱認證)
謝謝。
請問你能寫出整個堆棧跟蹤嗎? – Vinz243
謝謝vinz。我用完整的堆棧跟蹤進行編輯。 – madu