Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bcrypt module to save encrypted password in MongoDB using NestJS

How can I save an encrypted password to MongoDB?

P.S. I'm a beginner developer and still learning how to use NestJS

like image 769
sahil Avatar asked Oct 27 '25 10:10

sahil


1 Answers

If you are using TypeOrm there is a decorator name's @BeforeInsert()

@Entity("YourTable", { schema: "yourdb" })
export class YourTable {
   ...
   @BeforeInsert()
   async hashPassword() {
      this.password = await bcrypt.hash(this.password, Number(process.env.HASH_SALT));
   }
   ...
}
like image 82
3logy Avatar answered Oct 29 '25 23:10

3logy