【问题标题】:Express + Sequelize + bcrypt => question about scaffolding vis a vis inability to validate passwordExpress + Sequelize + bcrypt => 关于脚手架和无法验证密码的问题
【发布时间】:2022-02-11 20:59:03
【问题描述】:

我正在尝试学习如何在 Node.js 上控制 sequelize,并且我创建了一个简洁的注册/登录,atm 我无法访问我的 user.validPassword() 方法。

应用程序可以很好地包含所有进程',唯一的问题是密码验证。

/index.js

const initConnection = async function () {
    httpsServer.listen(port, () => {
        console.log(`listening at https://localhost:${port}`)
    });

    //db ops
    const db = require("./models");
    await db.sequelize.sync();

}

/models/index.js

const dbConfig = require("../config/db.config.js");

const Sequelize = require("sequelize");

const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
    host: dbConfig.HOST,
    dialect: dbConfig.dialect,
    operatorsAliases: false,

    pool: {
        max: dbConfig.pool.max,
        min: dbConfig.pool.min,
        acquire: dbConfig.pool.acquire,
        idle: dbConfig.pool.idle
    }
});

const db = {};

db.Sequelize = Sequelize;
db.sequelize = sequelize;

db.User = require("./User.model.js")(sequelize, Sequelize);
console.log({user: db.User});

module.exports = db;

/models/User.model.js

const bcrypt = require("bcrypt");

module.exports = (sequelize, Sequelize) => {
    return sequelize.define("user", {
        name: {
            type: Sequelize.STRING,
            allowNull: 0,

        },
        email: {
            type: Sequelize.STRING,
            allowNull: 0,
        },
        password: {
            type: Sequelize.STRING,
            allowNull: 0,
        },

    },{
        hooks: {
            beforeCreate: async (user) => {
                if (user.password) {
                    const salt = await bcrypt.genSaltSync(10, 'a');
                    user.password = bcrypt.hashSync(user.password, salt);
                }
            },
            beforeUpdate:async (user) => {
                if (user.password) {
                    const salt = await bcrypt.genSaltSync(10, 'a');
                    user.password = bcrypt.hashSync(user.password, salt);
                }
            }
        },
        instanceMethods: {
            validPassword: (password) => {
                return bcrypt.compareSync(password, this.password);
            }
        }
    });

/controllers/User.controller.js

const db = require("../models");
const path = require("path");
const User = db.User;
const Op = db.Sequelize.Op; //sql operators

exports.login = (req, res) => {
    const user = {
        email: req.body.email,
        password: req.body.password,
    };

    const isValidated = User.validPassword(user.password);
    console.log({isValidated});
};

错误:TypeError: User.validPassword is not a function

在过去的几个小时里,我一直在努力解决这个问题,但无济于事。

谢谢, 芽

【问题讨论】:

  • 两件事。 1. Sequelize V4 移除instanceMethods。您需要使用const User = sequelize.define(...); User.prototype.validPassword = (password) => {...}。 2.您正在定义一个实例方法,但您正在调用一个类方法。 User.classMethod()user.instanceMethod().

标签: javascript node.js express sequelize.js


【解决方案1】:

sequelize version >4 改变了实例方法的定义方式。

you can also refer to this link

const bcrypt = require("bcrypt");

module.exports = (sequelize, Sequelize) => {
    const User = sequelize.define("user", {
        name: {
            type: Sequelize.STRING,
            allowNull: 0,

        },
        email: {
            type: Sequelize.STRING,
            allowNull: 0,
        },
        password: {
            type: Sequelize.STRING,
            allowNull: 0,
        },

    }, {
        hooks: {
            beforeCreate: async (user) => {
                if (user.password) {
                    const salt = await bcrypt.genSaltSync(10, 'a');
                    user.password = bcrypt.hashSync(user.password, salt);
                }
            },
            beforeUpdate: async (user) => {
                if (user.password) {
                    const salt = await bcrypt.genSaltSync(10, 'a');
                    user.password = bcrypt.hashSync(user.password, salt);
                }
            }
        }
    })
    User.validPassword = (password) => {
        return bcrypt.compareSync(password, this.password);
    }
    return User;
}

【讨论】:

    猜你喜欢
    • 2016-02-27
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多