0
I am creating an api for reasons of studies, and I have a problem that bcrypt keeps returning false for what it seems to me, I used this same comparison function in another study project and it had worked. What could I be doing wrong so the comparison doesn’t work as it should? I even read the documentation and I believe I’m doing it right.
I’m creating my user that way:
router.post("/user", async (req, res) => {
const name = req.body.name
const email = req.body.email
const password = req.body.password
try {
await User.findOne({ where: { email: email } }).then(user => {
if (user == undefined) {
let salt = bcrypt.genSaltSync(10);
let hash = bcrypt.hashSync(password, salt)
User.create({
name: name,
email: email,
password: hash
})
res.sendStatus(201)
} else {
res.sendStatus(400)
}
})
} catch (err) {
console.log(err)
res.sendStatus(500)
}
})
And this is the authentication wheel I’m trying to validate the user
router.post("/auth", async (req, res) => {
var { email, password } = req.body
if (email != undefined) {
var user = await User.findOne({ where: { email: email } })
if (user != undefined) {
var correct = await bcrypt.compareSync(password, user.password)
if (req.body.password == correct) {
res.status(200)
res.json({ token: "Token falso" })
} else {
res.status(401)
res.json({ err: "credenciais inválidas" })
}
} else {
res.status(404)
res.json({ err: "O email enviado não existe" })
}
} else {
res.status(400)
res.json({ err: "O email enviado é inválido" })
}
})
that
if (req.body.password == correct){...}
shouldn’t beif (correct){...}
? because the way Voce wrote, Voce ta comparing any string (req.body.password
) with a Boolean (correct
), soon it will always befalse
and will not return thestatus(200)
never– Cmte Cardeal
Dude, that’s exactly what it was. vlw ai man, really, how do I close the question?
– Bussola