Skip to content

Bcrypt compare method is not returning true when hash password is stored in the database. #131

@Irtiza751

Description

@Irtiza751
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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions