Passport身份验证ReferenceError:未定义用户

时间:2017-03-15 16:31:19

标签: javascript node.js express passport.js

我正在尝试为我的网站制作一个身份验证系统。

但是当尝试使用任何组合登录时,我在网站上收到以下错误:

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 个答案:

答案 0 :(得分:1)

问题在于:

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.getUserByEmail中嵌套User.comparePassword函数,或使用promises正确解析它。

答案 1 :(得分:0)

提示消息显示用户未定义。这是用户.js中的消息错误呕吐。我想,您忘了提及您定义的护照策略名称。您在users.js中的代码应该是这样的:

 passport.use('local', new LocalStrategy({.......})