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

SCodeyRHODES

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.

Maulik Patel

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I have to compare user entered id with database id

Not able to read entered number in javascript

Compare uid and get username

Grails - Intercepting entered Username in LoginController's authfail

Writing regular expression that will filter entered username

Sending mail with the user entered username and password in Spring

How to loop this if the password or username entered is not correct

Pull the email related to the username entered in a textBox

How to Display a Default Username if no Username is Entered in Signalr Chat

how to get values to spring.boot.admin.client.username/ password from database?

Hash states to be able to compare them

Not able to compare day in momentjs with isSame()

Not able to compare NAN using Expression

Not able to compare Fridays date JavaScript

Python not able to compare the dates in dateframes

Not able to create database in phpMyAdmin

Not able to update MongoDB database

not able to delete database in postgres

Not able to add Items to Database

Not able to add todos to database

Check username exists in database or not

Not able to make a User with the same username that I deleted

How to compare the value entered through input box

How to compare the date entered by datetimepicker and current date

Compare a number that is entered by the user to a value (EBX)

php username that checks on database "username already taken"

Insert into MYSQL Database WHERE username = :username

Not able to call client method on OnDisconnected

Sensu Remediation : not able to Trigger on Client