无法将数据库中的用户名与客户输入的用户名进行比较。

huangapple go评论64阅读模式
英文:

Not able to compare username in database with that entered by the client

问题

所以,我正在尝试创建一个基本的身份验证应用程序,而不使用任何模块或身份验证策略。只需将用户凭据推送到MongoDb数据库,然后在用户尝试登录时每次与现有凭据进行比较。

链接

由于某种原因,我无法筛选出与用户输入的相同用户名的MongoDB文档。当我尝试在控制台上记录它时,我得到了一个查询,但我期望得到一个对象,以便可以比较req.body.passworduser.password

英文:

So I am trying to create a basic authentication app without using any modules or auth strategies. Just by pushing user credentials to a MongoDb database and then by comparing the entered credentials every time a user tries to login with the existing ones.

text

For some reason i am not able to filter out the mongodb document with the same username entered by the user. When i try to log it to the console i am getting a query , i am expecting an object so that both the req.body.password and user.password can be compared.

答案1

得分: 0

The User.findOne function returns a Query object, which represents the pending query operation. To retrieve the actual user document, you need to execute the query using .exec().

You can modify your function in this way:

app.post("/api/v1/login", (req, res) => {
  User.findOne({ username: req.body.username })
    .exec()
    .then((user) => {
      console.log(req.body.username);
      console.log(user);
      if (user) {
        const result = req.body.password === user.password;
        if (result) {
          res.redirect("/api/v1/dashboard");
        } else {
          res.status(404).json({ error: "entered password doesn't match" });
        }
      } else {
        res.redirect("/api/v1/register");
      }
    })
    .catch((error) => {
      console.log(error);
      res.status(500).json({ error: "An error occurred" });
    });
});

Let me know, If still the issue persists.

英文:

The User.findOne function returns a Query object, which represents the pending query operation. To retrieve the actual user document, you need to execute the query using .exec().

You can modify your function in this way:

app.post("/api/v1/login", (req, res) => {
  User.findOne({ username: req.body.username })
    .exec()
    .then((user) => {
      console.log(req.body.username);
      console.log(user);
      if (user) {
        const result = req.body.password === user.password;
        if (result) {
          res.redirect("/api/v1/dashboard");
        } else {
          res.status(404).json({ error: "entered password doesn't match" });
        }
      } else {
        res.redirect("/api/v1/register");
      }
    })
    .catch((error) => {
      console.log(error);
      res.status(500).json({ error: "An error occurred" });
    });
});

Let me know, If still the issue persist.

答案2

得分: 0

Mongoose的findOne返回一个Query,您需要执行它以获得包含实际对象的Promise。如果您将相关行更改为以下内容,应该可以正常工作:

app.post("/api/v1/login", async (req, res) => {
  var user = await User.findOne({ username: req.body.username }).exec();
  ...
英文:

Mongoose's findOne returns a Query, which you need to execute to get a Promise containing the actual object. If you change the relevant lines to the following, you should be fine:

app.post("/api/v1/login", async (req, res) => {
  var user = await User.findOne({ username: req.body.username }).exec();
  ...

huangapple
  • 本文由 发表于 2023年5月26日 16:42:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定