In this video session Saving User To MongoDB | Mongoose | Demo | 0009-22 we will explain how to save user data in mongo db and how to validate user at server using Mongoose User Schema.
First we will begin with password encryption, if we keep password in plain text it will be exposed to all. Good practice is to store password in database in encrypted manner. To do this we will install one more npm package bcrypt. For this we will run command npm i –S bcrypt.
Next important thing to take care is while saving any entity in DB, we should create schema for it. For this we will create models folder and declare all schema related things inside it in user.model.js file. Also to validate email id we can put regular expression (available on net) in this file.
Also we will define hashed password, created date and roles as well while putting validations in our schema.
Next in controller file, we will do hashing of password. This will delete actual password and push hashed version of password to save in database.
Now we can finally enter the user details in registration page of our product mart app and, on click of register button, User data will saved to db.
We can simply go to MongoDB user interface and check the entry of user in database.
In this way, we can save any data from our project application into our database. Here we have just focused on saving data of new user. In our next session User Login In Angular With MongoDB | Demo | 0009-23 we will try to validate already registered user data in db while logging in.