首页 > 解决方案 > nodejs登录系统密码未定义

问题描述

我正在用 Nodejs、Express 和 MongoDB 构建一个登录系统。当我单击登录按钮时,我不断被重定向到“您未通过验证”页面。我决定控制台记录密码以查看发生了什么,并且用户输入的密码以及存储在数据库中的密码由于某种原因出现为未定义(在 console.logs 处未定义)。我怎样才能解决这个问题?

app.post("/login", (req, res) => {
  //Get user fields
  const userEmail = req.body.loginEmail;
  const userPass = req.body.userPass;
  //Is user in database?
  User.find({ email: userEmail }, (err, user) => {
    if (!err) {
      //Compare password to database password
      bcrypt.compare(userPass, user.password, (err, result) => {
        console.log(user.pasword);
        console.log(userPass);
        //If user pass in database, check if verified & redirect to success
        if (userPass === user.password) {
          if (user.isVerified) {
            res.redirect("/success");
          } else {
            res.send(
              "You are not verified. Please check your email to access your account."
            );
          }
        } else {
          res.send("Incorrect password");
        }
      });
    } else {
      res.send(err);
    }
  });
});

标签: javascriptnode.jsmongodbauthentication

解决方案


推荐阅读