Creating User upon Request in MEAN Stack
Our previous section successfully created the back-end routes for our login and signup forms and our mongoose model for our users with proper validation. Now, we need to save our users whenever we get a request sent to our new route. We will use the following steps to create the user whenever we get a new request reaching the signup route.
1) We will go back to our js file located in the routes folder and import our user model created in our previous section.
4) The way in which we set the properties is very bad because we would store the password unencrypted or in raw form. If anyone ever accessed to our database, he can read all the user passwords in raw form. So, we don't store the password like body.password. Instead of this, we need to hash our password. We need to encrypt it in a way that it cannot be decrypted, so that it cannot be reversed. For that, we will install another package, i.e., bcrypt. This package offers encryption functionality, which we can use in NodeJS. We will install it using the following command:
5) After installing it, we will import it in our user route to use it. Now, we will create a new user with that. We will use the hash() method of the bcrypt package, and this function takes an input, and that input is the value that we want to hash.
6) After that, we pass the salting rounds, which means the higher the number here, the longer it will take, but the safer it will be because it uses mathematics to generate a random number and to generate the hash, which is secure.
In the above code, we used ten that is high enough to yield a secure hash.
7) Now, we will get the callback, which will fire whenever it is done. Alternatively, we chain a promise, so we use the then() block here. This then block will yield us the hash once it is done. We will create the new user inside this then block like this:
8) Now, for the password, we will not store the raw password. Instead of this, we will store the generated hash like as:
9) We have created the user, and now we will call the save() to save the user to the database. We will attach the then block, which will execute when it succeeded. In this then block, we will create or send a response where we set the status code to 201 because we created a new user and where we send back some json data like this:
10) We will also add the catch block because we would not add all the error handling logic yet. So, in this catch block, we will send back a status of 500 and a json message where we have our error property, which holds the error we get.
Now, if we try to restart the server, we can get an error "cyclic dependency" was detected if you didn't get this error, then you can ignore this. To solve this error, we will go back to our app.js file, and on that MongoDB link to our back-end database, we will remove the retryWrites=true&w=majority.
Everything is looking good, and with that, we should be able to create new users and store them in our database. In the next section, we will connect angular with the backend routes.