There is a Crypto class by custom made in nestjs.
import * as bcrypt from 'bcrypt';
export class Crypto {
constructor() {
}
public async hash(target: string, salt: number): Promise<string> {
return await bcrypt.hash(target, salt);
}
public async compareHash(
target: string,
hash: string,
): Promise<boolean> {
return await bcrypt.compare(target, hash);
}
}
And I made this instance in a service of nestjs as below.
public async create(createConfigDto: CreateConfigDto): Promise<IConfig> {
const crypto = new Crypto();
const hashedPassword = await crypto.hash(createConfigDto.password, 10);
const newConfig = await new this.configModel({
...createConfigDto,
password: hashedPassword,
});
return newConfig.save();
}
public async read() {
const crypto = new Crypto();
const hashedPassword = await crypto.compare(createConfigDto.password, hash);
...
}
or I can do this crypto instance outside of create and read method to avoid duplicate call instance.
But my major question is about there is more efficient maintaining covention of nestjs for this case.
Could you give me some advice for me?
Thank you for reading my question.