微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

bcrypt 不散列密码

如何解决bcrypt 不散列密码

我在使用 bcrypt 散列密码时遇到问题。我使用 5.0.0 版,它适用于其他项目,但现在只是 ganerate 盐而不是散列。这就是代码,我以前用过它,效果很好:

const { saltRounds } = require('../config');

module.exports = (mongoose,bcrypt) => {

    const { Schema,model: Model } = mongoose;
    const { String,Number } = Schema.Types;

    const userSchema = new Schema({
        username: {
            type: String,required: true,unique: true
        },email: {
            type: String,password: {
            type: String,},points: {
            type: Number
        },});

    userSchema.methods = {
        comparePasswords(password) {
            return bcrypt.compare(password,this.password);
        }
    };

    userSchema.pre('save',function (next) {
        // Before we save the new user,we should crypt the password and save it like that
        let base = this
        if (!this.isModified('password')) {
            next();
            return;
        }

        bcrypt.genSalt(saltRounds)
            .then(
                salt => {
                    console.log(salt,base.password)
                    bcrypt.hash(base.password,salt)
                        .then(hash => {
                            console.log(`Hash generated: ${hash}`)
                            base.password = hash
                            next()
                        });
                }
            ).catch(err => console.log(err));

    })

    return Model('User',userSchema)
}

并且它没有捕捉到任何错误。我将不胜感激任何帮助。谢谢!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。