你如何检测 typeorm 中的密码等属性是否已更改
How do you detect if an attribute like password has changed in typeorm
在 typeorm 中,我尝试使用订户装饰器在持久保存到数据库之前对用户密码进行哈希处理。不幸的是,我在文档中找不到参考资料。
在sequelizejs中,我使用了下面的代码,
User.hashPassword = (user, options) => {
if (!user.changed('password')) {
return null;
}
// hash password
return Bcrypt.hash(user.get('password'), SALT_ROUNDS)
.then(hash => user.set('password', hash));
};
现在,我正在尝试将代码迁移到 typeorm
,我的翻译大致是
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
// conditional to detect if password has changed goes here
this.password = bcrypt.hashSync(this.password, SALT_ROUNDS);
}
问题是,我卡在了 !user.changed('password')
。 typeorm
中是否有等效函数可以在不推出我自己的解决方案的情况下执行此操作?
这个问题的答案是在@adetoola 自己的 issue 中找到的。
您可以使用 @AfterLoad
加载用户密码并检查当前密码是否不同:
@Entity()
export class User extends BaseEntity {
@PrimaryColumn()
public username: string;
@Column()
public password: string;
@Column({ nullable: true })
public jwtToken: string;
private tempPassword: string;
@AfterLoad()
private loadTempPassword(): void {
this.tempPassword = this.password;
}
@BeforeUpdate()
private encryptPassword(): void {
if (this.tempPassword !== this.password) {
//
}
}
你可以试试这个:
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
if (this.password) {
this.password = createHmac('sha256', this.password).digest('hex');
}
}
我只是检查 DTO 中是否存在密码(在更新和插入之前)。如果它存在,我应该散列它。
----------
private tempPassword: string
/// commit to handle the password if i not change it it will be not encription
@AfterLoad()
private loadTempPassword(): void {
this.tempPassword = this.password;
}
@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
// cheack if that password changing or not
if (this.tempPassword !== this.password) {
try {
this.password = await bcrypt.hash(this.password, 10)
} catch (e) {
throw new InternalServerErrorException('there are some issiue in the hash')
}
}
}
我们可以从列中选择密码
@Column('string', { select: false })
password:string
然后我们尝试散列
we check if the password is found or not
if (this.password) {
//make hash
}
或其他方式
它应该使 private tempPassword: string
@AfterLoad()
private loadTempPassword(): void {
this.tempPassword = this.password;
}
或
@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
// cheack if that password changing or not
if (this.password) {
if (this.tempPassword !== this.password) {
try {
this.password = await bcrypt.hash(this.password, 10)
} catch (e) {
throw new InternalServerErrorException('there are some issiue in the hash')
}
}
}
在 typeorm 中,我尝试使用订户装饰器在持久保存到数据库之前对用户密码进行哈希处理。不幸的是,我在文档中找不到参考资料。
在sequelizejs中,我使用了下面的代码,
User.hashPassword = (user, options) => {
if (!user.changed('password')) {
return null;
}
// hash password
return Bcrypt.hash(user.get('password'), SALT_ROUNDS)
.then(hash => user.set('password', hash));
};
现在,我正在尝试将代码迁移到 typeorm
,我的翻译大致是
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
// conditional to detect if password has changed goes here
this.password = bcrypt.hashSync(this.password, SALT_ROUNDS);
}
问题是,我卡在了 !user.changed('password')
。 typeorm
中是否有等效函数可以在不推出我自己的解决方案的情况下执行此操作?
这个问题的答案是在@adetoola 自己的 issue 中找到的。
您可以使用 @AfterLoad
加载用户密码并检查当前密码是否不同:
@Entity()
export class User extends BaseEntity {
@PrimaryColumn()
public username: string;
@Column()
public password: string;
@Column({ nullable: true })
public jwtToken: string;
private tempPassword: string;
@AfterLoad()
private loadTempPassword(): void {
this.tempPassword = this.password;
}
@BeforeUpdate()
private encryptPassword(): void {
if (this.tempPassword !== this.password) {
//
}
}
你可以试试这个:
@BeforeInsert()
@BeforeUpdate()
hashPassword() {
if (this.password) {
this.password = createHmac('sha256', this.password).digest('hex');
}
}
我只是检查 DTO 中是否存在密码(在更新和插入之前)。如果它存在,我应该散列它。
----------
private tempPassword: string
/// commit to handle the password if i not change it it will be not encription
@AfterLoad()
private loadTempPassword(): void {
this.tempPassword = this.password;
}
@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
// cheack if that password changing or not
if (this.tempPassword !== this.password) {
try {
this.password = await bcrypt.hash(this.password, 10)
} catch (e) {
throw new InternalServerErrorException('there are some issiue in the hash')
}
}
}
我们可以从列中选择密码
@Column('string', { select: false })
password:string
然后我们尝试散列
we check if the password is found or not
if (this.password) {
//make hash
}
或其他方式 它应该使 private tempPassword: string
@AfterLoad()
private loadTempPassword(): void {
this.tempPassword = this.password;
}
或
@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
// cheack if that password changing or not
if (this.password) {
if (this.tempPassword !== this.password) {
try {
this.password = await bcrypt.hash(this.password, 10)
} catch (e) {
throw new InternalServerErrorException('there are some issiue in the hash')
}
}
}