简体   繁体   English

Bcrypt比较问题nodejs

[英]Bcrypt compare issue nodejs

I've got myself two functions, first is responsible for adding a user model to database and second one for comparing passwords.我有两个功能,第一个负责将用户 model 添加到数据库,第二个负责比较密码。 But.. comparing never works..但是..比较永远行不通..

module.exports.signup = function (req, res) {
if (req.body == null) {
    res.status(400);
    return res.end('Bad juju');
} else {
    let exists;
    User.findOne({ username: req.body.username }),
        (err, doc) => {
            if (doc) {
                exists = true;
                return;
            }
        };
    if (exists) {
        res.setHeader('user-exists', true);
        res.redirect('/signup');
    } else {
        bcrypt.hash(req.body.password, 10, function (hashE, hash) {
            if (hashE) {
                throw hashE;
            }
            new User({
                username: req.body.username,
                email: req.body.email,
                password: hash,
            }).save();
        });
        return res.redirect('/login');
    }
}
};

module.exports.login = function (req, res) {
if (req.body.tosignup) {
    return res.redirect('/signup');
}
if (req.body == null) {
    res.status(400);
    return res.end('Bad request');
} else {
    User.findOne({ username: req.body.username }, (err, doc) => {
        if (err) throw console.log(err);
        console.log(doc.password);
        console.log(req.body.password);
        bcrypt.hash(req.body.password, 10, (err, s) => {
            console.log(s);
        });
        bcrypt.compare(req.body.password, doc.password, (err, succ) => {
            if (err) {
                throw err;
            }
            console.log(err);
            console.log(succ);
            if (succ) {
                res.setHeader('username', doc.username);
                return res.redirect('/welcome');
            } else {
                res.setHeader('password-wrong', true);
                return res.redirect('/login');
            }
        });
    });
}
};

I've looked for different sources and all of them told that this one method is the correct one, but every time I try using it, it just doesn't work我寻找了不同的来源,所有人都告诉我这种方法是正确的,但是每次我尝试使用它时,它都不起作用

I had a similar problem using bcrypt in nodejs.我在 nodejs 中使用 bcrypt 时遇到了类似的问题。 To solve the problem i switched from npm bcrypt to npm bcryptjs ( https://www.npmjs.com/package/bcryptjs ) and used the following:为了解决这个问题,我从 npm bcrypt 切换到 npm bcryptjs ( https://www.npmjs.com/package/bcryptjs )并使用以下内容:

NPM require: NPM 要求:

const bcrypt = require('bcryptjs');

To compare the passwords you can use the following code:要比较密码,您可以使用以下代码:

async function compareIt(password, hashedPassword) {
  const validPassword = await bcrypt.compare(password, hashedPassword);
  return validPassword;
}

compareIt(password, passwordBD).then(v => {
    if (v == true) {
        console.log("Equal");
    } else {
        console.log("Not equal");
    }
});

To hash the password you can use this function:到 hash 的密码可以使用这个 function:

async function hashIt(password) {
  const salt = await bcrypt.genSalt(6);
  const hashed = await bcrypt.hash(password, salt);
  return hashed;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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