typescript - 如何使用 typescript 和 bcrypt 在 sequelize 模型中添加方法?

标签 typescript sequelize.js bcrypt

我正在用 typescript 创建一个项目,但在使用 bcrypt 和 sequelize 时遇到了麻烦。
为了加密密码,我输入了以下代码:

import Sequelize from "sequelize";
import { sequelize } from "../dbpostgredatabase";
import bcrypt from "bcrypt";

export const User = sequelize.define(
  "User",
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false,
    },

    password: {
      type: Sequelize.STRING,
      allowNull: false,
      set(value: string) {
        const hash = bcrypt.hashSync(value, 8);
        this.setDataValue("password", hash);
      },
    },

    lastname: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    email: {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true,
    },
    date: {
      type: Sequelize.DATE,
      allowNull: false,
    },
    admin: {
      type: Sequelize.BOOLEAN,
      defaultValue: false,
    },
  },

  {
    timestamps: false,
    createdAt: false,
    updatedAt: false,
  }
);

export default User;
我需要那个哈希值,但我现在不知道如何获取它,我只需要一种方法来比较密码

最佳答案

import Sequelize from "sequelize";
import { sequelize } from "../dbpostgredatabase";
import bcrypt from "bcrypt";

export const User = sequelize.define(
  "User",
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: Sequelize.STRING,
      allowNull: false,
    },

    password: {
      type: Sequelize.STRING,
      allowNull: false,
      set(value: string) {
        const hash = bcrypt.hashSync(value, 8);
        this.setDataValue("password", hash);
      },
    },

    lastname: {
      type: Sequelize.STRING,
      allowNull: false,
    },
    email: {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true,
    },
    date: {
      type: Sequelize.DATE,
      allowNull: false,
    },
    admin: {
      type: Sequelize.BOOLEAN,
      defaultValue: false,
    },
  },

  {
    timestamps: false,
    createdAt: false,
    updatedAt: false,
  }
);

// add static method here
User.hashPassword = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
}

User.isValidPassword = function(password, hash) {
    return bcrypt.compareSync(hash, password);
}

export default User;

关于typescript - 如何使用 typescript 和 bcrypt 在 sequelize 模型中添加方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67694882/

相关文章:

sequelize.js - 我得到的这个 "defineCall is not a function"错误是什么?

javascript - Sequelize 返回比请求更多的字段

node.js - Nodejs 的 bcrypt 实现中的 "B4c0/\/"是什么?

authentication - 将用户身份验证迁移到 Firebase 身份验证

c# - BCrypt 根据数据库中的密码验证密码

reactjs - 如何在 TypeScript 中通过组件注入(inject)将 defaultProps 添加到通用功能组件?

typescript - 组件挂载失败 : template or render function not defined: Vue Dynamic import, 延迟加载

sequelize.js - Sequelize 迁移是否应该更新模型文件?

typescript - 是否可以为方法装饰器选项提供类型安全

javascript - 如何在复选框内显示数组元素 - ionic