From the course: Advanced Express

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Adding password encryption and validation to a Mongoose model

Adding password encryption and validation to a Mongoose model

From the course: Advanced Express

Start my 1-month free trial

Adding password encryption and validation to a Mongoose model

- [Instructor] Now that we know a little bit about the theory of password encryption, let's implement it real quick in our model. So I open server models, UserModel.js, and first of all, I want to install bcrypt, so I run NPM install dash dash save bcrypt. Of course I also have to require it, so add const bcrypt equals require bcrypt. You also want to add a constant that holds the number of salt rounds, so add salt underscore rounds equals 12. Next, after the schema was defined, I will now implement a so-called presave hook for Mongoose, so I add UserSchema dot pre save, so this is a function that will run every save for document, and there I add async function. We cannot use fat arrow syntax here because we have to use this in the function then and this refers then to the actual object. I give the function name, preSave, and this gets next callback, and in there I will now add const user equals, and this is now the usage of this, so user refers now to the actual to be saved object…

Contents