简体   繁体   中英

Bcrypt.compare not comparing the passwords

I have a bcrypt.compare function, but it couldn't compare the passwords rather any password entered was able to return status ok. See the code below and help me out.Thanks

bcrypt.compare(req.body.password, users.password).then(
    (valid) => { 
        if (!valid) {
            return res.status(401).json({
                error: new Error('Incorrect password!')
            });
        }
        const token = jwt.sign(
            { userId: users._id },
            'RANDOM_TOKEN_SECRET',
            { expiresIn: '24h' }
        );
        res.status(200).json({
            userId: users._id,
            token: 'token'
        });
    }
).catch(
    (error) => {
        res.status(500).json({
            error: error
        });
    }
);

I once had some like this before. How long is your password column or table? Bcrypt hashes the password with an string greater than 60 chars. Is it your column on that range?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM