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
From the course: Advanced Express
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Setting up a hosted MongoDB server4m 22s
-
(Locked)
Connecting to MongoDB3m 27s
-
(Locked)
Adding MongoDB and Mongoose to an Express project4m 54s
-
(Locked)
Creating a user schema for Mongoose6m 21s
-
(Locked)
Using bcrypt to hash and validate passwords1m 31s
-
(Locked)
Adding password encryption and validation to a Mongoose model5m 12s
-
(Locked)
Creating a user registration route5m 36s
-
(Locked)
Testing the form and reviewing the data in MongoDB1m 33s
-
(Locked)
-
-
-
-