I am having trouble with Bcrypjs.compare() when run on a handle submit of a login form. The form is created using formik.
As the code below displays, when submitting username and passkey, it first finds the user in the databass, along with the stored hashed passkey and then also the stored salt.
The stored salt is used in the handlesubmit to hash the passkey input from the form before using bcrypt.compare() to compare the hashed input and the stored hashed passkey.
I console log both parts and recieve an identical code in each cases.
However our compare method still returns false.
Any ideas on what is gong wrong?
const handleSubmit = async(e) => {
e.preventDefault()
if (Object.keys(errors).length > 0){
alert("please fill out all fields")
setFailedValidation(true)
} else {
setFailedValidation(false)
console.log("running hansdlesubmit") //need to destructur
const username = values.username;
let storedUser = await axios.get(`http://localhost:3001/users/${username}`)
console.log("stored user is", storedUser.data)
const hashedPW = await bcrypt.hash(values.password, storedUser.data.salt)
console.log(hashedPW, storedUser.data.passkey)
bcrypt.compare(hashedPW, storedUser.data.passkey, function(err, res) {
if (err){
// handle error
console.log(err)
}
if (res) {
// Send JWT
console.log("its a match")
} else {
console.log("did not match")
// response is OutgoingMessage object that server response http request
}
});
}