Using BCrypt with Sequelize Model

前端 未结 3 1048
礼貌的吻别
礼貌的吻别 2020-12-05 10:47

I\'m trying to use the bcrypt-nodejs package with my sequelize model and was tring to follow a tutorial to incorporate the hashing into my model, but I\'m getti

3条回答
  •  天涯浪人
    2020-12-05 11:30

    Methods should be provided in the "options" argument of sequelize.define

    const bcrypt = require("bcrypt");
    
    module.exports = function(sequelize, DataTypes) {
        const User = sequelize.define('users', {
            annotation_id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true
            },
            firstName: {
                type: DataTypes.DATE,
                field: 'first_name'
            },
            lastName: {
                type: DataTypes.DATE,
                field: 'last_name'
            },
            email: DataTypes.STRING,
            password: DataTypes.STRING
        }, {
            freezeTableName: true,
            instanceMethods: {
                generateHash(password) {
                    return bcrypt.hash(password, bcrypt.genSaltSync(8));
                },
                validPassword(password) {
                    return bcrypt.compare(password, this.password);
                }
            }
        });
    
        return User;
    }
    

提交回复
热议问题