Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

643
Visualizações
Bcrypt compare method is not returning true when hash password is stored in the database
userSchema.statics.findByCredentials = async (email, password) => {
    const user = await User.findOne({ email })

    if (!user) {
        throw new Error('no user found!')
    }

    const isMatch = await bcrypt.compare(password, user.password)

    if (!isMatch) {
        throw new Error('invalid credentials!')
    }

    return user
}

The above code is matching the hash password that is stored in the database with a plain text password but it always returns false. so I tried this & it's working, could anyone explain to me; why this code is working & the above one is not working?

const bcrypt = require('bcryptjs')
const encription = async () => {
const password = 'abc1234';
console.log('plain password: ', password);

const hashPass = await bcrypt.hash(password, 8);
console.log(`hash password: ${hashPass}`);

const isValidPass = await bcrypt.compare(password, hashPass);
console.log(`is valid: ${isValidPass}`);
}

encription();
over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda