如何使用护照修复登录中的错误

时间:2019-01-04 13:13:38

标签: javascript node.js mongodb login

我已经通过护照实现了登录功能。编译时出现错误。第一个代码部分指示了我的index.js。该文件存储在路由文件中。我的问题是为什么它不起作用。

//index.js File

router.get('/login', function(req, res, next) {
res.render('login', { title: 'Login' });
});

router.post('/login',
passport.authenticate('local',{failureRedirect:'views/login'}),
function(req, res) {
req.flash('success','You are successfully logged in');
console.log('successful logged in');
res.redirect('/home');
}
);

passport.serializeUser(function(user, done) {
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.getUserById(id, function(err, user) {
done(err, user);
});
});

passport.use(new LocalStrategy(
function(username, password, done) {
User.getUserByUsername(username, 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.js File 

  var UserSchema = mongoose.Schema({
  username: {
  type: String
  },
  pass: {
  type: String
  }

  });


  var user = module.exports = mongoose.model('user',UserSchema);

  module.exports.getUserByUsername = function(username, callback){
  var query = {username: username};
  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);
  });
  }


  //Login Form
  <form action="/login" method="POST">
  <input type="text" name="username" value=""><br>
  <input type="password" name="password" value=""><br>
  <input type="submit" name="submit" value="Login">
  </form>

这实际上是我遵守时得到的

错误:无法在视图目录“ M:\ New folder \ SuperApp \ views”中查找视图“错误”

1 个答案:

答案 0 :(得分:0)

这与护照无关,但是您的框架(express.js吗?)在目录“ M:\ New folder \ SuperApp \ views”中找不到“错误”视图

我猜您是在代码中的某个地方调用res.render('error')