Javascript NestJS哈希密码
我试图在用户实体文件中散列密码,我从另一个项目复制了代码,该代码在该项目上有效,但在这个项目上,它不起作用 在实体中:Javascript NestJS哈希密码,javascript,hash,passwords,nestjs,Javascript,Hash,Passwords,Nestjs,我试图在用户实体文件中散列密码,我从另一个项目复制了代码,该代码在该项目上有效,但在这个项目上,它不起作用 在实体中: @Column() password: string; @BeforeInsert() async hashPassword() { this.password = 'hashed password'; } 在职: async create(user: DeepPartial<User>) { const newUser = th
@Column()
password: string;
@BeforeInsert()
async hashPassword() {
this.password = 'hashed password';
}
在职:
async create(user: DeepPartial<User>) {
const newUser = this.userRepository.create(user);
return this.userRepository.save(newUser);
}
异步创建(用户:DeepPartial){
const newUser=this.userRepository.create(user);
返回this.userRepository.save(newUser);
}
它创建了一个用户,但密码没有散列。如果您使用的是Sequelize ORM,我这样做的方式是返回到我的数据库提供程序,并在初始化Sequelize模型后添加它
export const databaseProviders = [
{
provide: ioc.sequelizeProvider,
useFactory: async () => {
const sequelize = new Sequelize({
dialect: 'mysql',
host: databaseConstants.host,
port: databaseConstants.port,
username: databaseConstants.username,
password: databaseConstants.password,
database: databaseConstants.database,
});
sequelize.addModels([Profile]);
await sequelize.sync();
Profile.beforeSave((profile, options) => {
const hashPassword = crypto.createHmac('sha256', profile.password).digest('hex');
profile.password = hashPassword;
});
return sequelize;
},
},
];
如果您使用的是Sequelize ORM,我这样做的方式是返回到我的数据库提供程序,并在初始化Sequelize模型后添加它
export const databaseProviders = [
{
provide: ioc.sequelizeProvider,
useFactory: async () => {
const sequelize = new Sequelize({
dialect: 'mysql',
host: databaseConstants.host,
port: databaseConstants.port,
username: databaseConstants.username,
password: databaseConstants.password,
database: databaseConstants.database,
});
sequelize.addModels([Profile]);
await sequelize.sync();
Profile.beforeSave((profile, options) => {
const hashPassword = crypto.createHmac('sha256', profile.password).digest('hex');
profile.password = hashPassword;
});
return sequelize;
},
},
];
请不要使用SHA256进行密码哈希。有专门为密码设计的哈希函数,如Bcrypt、Argon2或PBKDF2。请参阅请勿使用SHA256进行密码哈希。有专门为密码设计的哈希函数,如Bcrypt、Argon2或PBKDF2。看