2 / 3
Jul 2024

i have this line of code,i just want to know if it’s good practice to hash the password with the pre-save middleware like code below.? i mean is there is a securty issue about this or put something in the .env file

UserSchema.pre("save", async function (next) { try { const hashedhPassword = await bcrypt.hash(this.password, 10); this.password = hashedhPassword; } catch (err: any) { next(err); } });
5 months later

yeh not the bad approach to hashing the password in pre save just implement some check in the schema and don’t make it too simple for hashing