last day (15 days later) » 

5:59 AM
-2
Q: Node.js bcrypt compare problem only return false

GRINDI want to do my login API with Node.js and MongoDB and when I compare my pass from input to the one that's in the db I always get false I read other post on StackOverFlow but didn't helped me. I think I also found the problem: When I use hash on my password input to check it manually with the one...

 
what is broken?
ik is also await and i use then
 
How come user.password be a Promise object? I'd reckon on providing mode code we'll understand your intentions
 
i only take the pass from db and compare it wwith the one from req.body
const match = await bcrypt.compare(password, user.password) console.log(match) // return false
 
My best guess is that you had stored the password as plain text (not as hash one) and then you tried to compare. bcrypt.compareSync(pwd, hashedPwd): pwd is the plaintext password received from client, for example. hashedPwd - the hashed password to compare with. Typically your db-stored password.
Had you used bcrypt.hash() before you used bcrypt.compare()?
 
i stored my pass as hased pass in the db
what do you mean to use it before compare can i see some code?
 
5:59 AM
Let password=1234. First, you store it in DB as: bcrypt.hashSync(password, 8). Then, to compare: bcrypt.compareSync(plainPassword, passwordFromDB). This is how it goes. It would return true if you did give correct password.
 
yeah this is what i have and i get false on compare
can i get the good code then?
 
The line of code you provided, is 100% correct. You should provide more code, because this line is not the problem.
 
what should i provide is ok the code where i hash the user in my registration api?
 
The "Good" code would be either const match = bcrypt.compareSync(password, user.password) or const match = await bcrypt.compare(password, user.password).
 
yeah thats what i have now and is the same bro
 
5:59 AM
Please provide at least partial code of your user model, and the specific code in which your store the user password in DB.
 
and when i hash the pass i have ``` user.password = bcrypt.hashSync(process.env.secret, 10) ```
my user model only have email username password type string with required true
 
Please provide the requested above so I could answer you a full answer
 
but how i said in the title i think i found the bug when i console.log the pass from input i try to hash it and compare manualy and on every request is changed i think thats the problem but idk how to fix
sure 2 seconds..
edited look above
 
You did not provide the part where you store password (registration probably)
 
going to make one more edit in 1 minute sorry
i made the edit
 

last day (15 days later) »