0

how to fix?, password is 123456 is same but is not working to compare.
I have checked that they are all correct.

//const bcrypt = require('bcrypt')
const auth_signin_email = async (req, res) => {
    const email = req.body.email;
    const password = req.body.password;
    if (!email || !password) {
        res.status(400)
        res.json({message: 'Email invaild'})
    };
    //sequelize mysql
    oldData = await auth.findOne({
        where: {email: email}
    })
    // check oldData == null
    if (oldData == null) {
        res.json({messages: 'Not found Email, Please Try again'})
    };
    /*password is 123456*/
    bcrypt.compare(password, oldData.password, function (err, isLogged) {
        //this False
        if (isLogged) {
            const token = jwt.sign({data: oldData}, secret, {expiresIn: '12h',
            algorithm:'PS384'});
            res.json({token: token})
        } else {
            res.status(400)
            res.json({message: 'Wrong Email or Password'})
        }
    })
}

password = 123456
oldData.password = hash of 123456

bcrypt.compare(password, oldData.password, function (err, isLogged) {
        //this False
        if (isLogged) {
            const token = jwt.sign({data: oldData}, secret, {expiresIn: '12h',
            algorithm:'PS384'});
            res.json({token: token})
        } else {
            res.status(400)
            res.json({message: 'Wrong Email or Password'})
        }
})
1
  • Are types of those string ? You should check the types with typeof method. Commented Oct 8, 2022 at 18:13

1 Answer 1

1

it might be because your oldData.password is a hashed string '123456' and the value you are using in compare is a number 123456 which results in isLogged to be false.

You may try password.toString() in the bcrypt.compare function

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.