护照认证 ReferenceError: 用户未定义
2017-03-15
8107
我正在尝试为我的网站创建一个身份验证系统。
但是当我尝试使用 任意 组合登录时,我在网站中收到以下错误:
ReferenceError: user is not defined
user is not defined
ReferenceError: user is not defined
at Strategy._verify (/home/jarno/0__projects/nodejs/EasyOrders/routes/users.js:76:36)
at Strategy.authenticate (/home/jarno/0__projects/nodejs/EasyOrders/node_modules/passport-local/lib/strategy.js:90:12)
at attempt (/home/jarno/0__projects/nodejs/EasyOrders/node_modules...
在控制台中:
POST /users/login 500 204.677 ms - 3063
events.js:160
throw er; // Unhandled 'error' event
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (/home/jarno/0__projects/nodejs/EasyOrders/node_modules/express/lib/response.js:719:10)...
我的代码:
Users.js
passport.use(new LocalStrategy({
usernameField: 'email'
},
function(email, password, done) {
User.getUserByEmail(email, function(err, user) {
if(err) throw err;
if(!user) {
return done(null, false, {message: 'Unknown User'});
}
});
User.comparePassword(password, user.password, function(err, isMatch) {
if(err) throw err;
if(isMatch) {
return done(null, user);
} else {
return done(null, false, {message: 'Invalid password'});
}
});
}
));
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});
router.post('/login',
passport.authenticate('local', {successRedirect: '/', failureRedirect:'/users/login', failureFlash: true}),
function(req, res) {
res.redirect('/');
});
module.exports = router;
User.js 模型:
var UserSchema = mongoose.Schema({
email: {
type: String,
index: true
},
name: {
type: String
},
password: {
type: String
},
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.createUser = function(newUser, callback) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
newUser.save(callback);
});
});
}
module.exports.getUserByEmail = function(email, callback) {
var query = {email: email};
User.findOne(query, callback);
}
module.exports.getUserById = function(id, callback) {
User.findById(id, callback);
}
module.exports.comparePassword = function(candidatePassword, hash, callback) {
bcrypt.compare(candidatePassword, hash, function(err, isMatch) {
if(err) throw err;
callback(null, isMatch);
});
}
2个回答
问题出在这里:
User.getUserByEmail(email, function(err, user) {
if(err) throw err;
if(!user) {
return done(null, false, {message: 'Unknown User'});
}
});
User.comparePassword(password, user.password, function(err, isMatch) {
if(err) throw err;
if(isMatch) {
return done(null, user);
} else {
return done(null, false, {message: 'Invalid password'});
}
});
您试图使用尚未定义的 user.password。 您需要将 User.comparePassword 函数嵌套在 User.getUserByEmail 中,或使用承诺正确解决它。
yBrodsky
2017-03-15
提示信息显示用户未定义。这是 users.js 中抛出的消息错误。我认为,您忘记提及您定义的护照策略的名称。您在 users.js 中的代码应该是这样的:
passport.use('local', new LocalStrategy({.......})
Wejd DAGHFOUS
2017-03-15