Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getUD function(query file not passing correct response back to handler) #81

Open
rooberrydev opened this issue Aug 16, 2019 · 0 comments

Comments

@rooberrydev
Copy link

In handler.js line 110-124, getUD is being called, but dbPassword was not what you were expecting it to be(hashed password from the database).

getUD(username, password)
      .then(dbPassword => {
        console.log(dbPassword);
      })
      .then(result => {
        if (result == false) {
          console.log(result);
        } else {
          res.writeHead(301, {
            Location: "/",
            "Set-Cookie": `jwt=${cookie}`
          });
          res.end();
        }
      });

i took a look at where getUD is defined(getUD.js),
and you're resolving the promise with the response that comes back from the sql query.
this response is always a massive object, and your password is actually hidden inside the .rows property of this object, which is an array with objects inside it that represent the rows returned by your sql query.

const getUD = (username, password) => {
  return new Promise((resolve, reject) => {
    dbConnection
      .query(`SELECT password FROM login WHERE username= '${username}';`)
      .then(dbPassword => {
        // passwordHandling.comparePasswords(
        //   dbPassword[1],
        //   dbPassword[0][0].password
        // );
        resolve(dbPassword);
        console.log("dbPassword:", dbPassword);
        // console.log(dbPassword[1], dbPassword[0][0].password);
      })
      .catch(err => reject(err));
  });
};

dbPassword here isnt your database password, its an object with the value you want nested inside 😄
I've rewritten this to access the password and use it to resolve the response.

const getUD = (username, password) => {
  return new Promise((resolve, reject) => {
    dbConnection
      .query(`SELECT password FROM login WHERE username= '${username}';`)
      .then(sqlresponse => {
        console.log('response from password sql query', sqlresponse);
        //this is a massive object and you want the .rows property
        console.log('password is: ',sqlresponse.rows[0].password);
        resolve(sqlresponse.rows[0].password);
      })
      .catch(err => reject(err));
  });
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant