In this video.
Let's go ahead to register a user.
So let's get into the controller of the users and here we go.
So let's start with the registration.
Before we get into that, let's look at the steps that we are going to follow.
Step number one is you want to check if the user is not registered.
So here, check if email exists that first step.
The second step is that you want to check if a user is trying to submit an empty data verrastro mongoose
who through an error.
But we can also check for that inside the controller before getting to the model.
So here we go.
Check if failed.
And.
And lastly is we want to hash the user password.
So these are the three steps that we are going to follow.
So first is let's bring in the user model.
So let's see, the auto import is working fine.
If it doesn't work, let me show you some tricks.
Go to the file.
That is the model here.
This is what we want.
We save it.
And now back to the file where you want to have the auto import.
And by typing user, you can see that it is working like a magic.
So here we go.
So first of all, let's structure the properties that we want to create from the request body as cons
equal to wreck dirt body.
And inside here we need the foo name, the password, and then the email.
So here let's check if user is found by email.
So the const user found is equal to await the user model and you're going to use find one.
And if find one we can find by any property for this one you want to find by email.
So here, let's go ahead and through an error if a user is found.
But because we have not tackled the error handling, we are going to send some message to the user by
using rest.
Jason So here we go.
If user found, then we can say return from this function and then let's use rest.
Jason And then we are going to say the message by saying that user already.
Exists.
So let's save it.
And now let's work with the empty field.
Well, so we are going to check if there is no email or there is no password or there is no full name
and then return rest docs.
Jason and then message.
You are going to say that, please.
Provide all fuel.
Well, we can still use a third party package like Express Validator to do all these ones.
But I just want to bring this one here.
So it's is not going to be an optional, though.
So after that, at this point, we can go ahead and then register the user.
But you don't want to save the PIN password into the database.
Because of that, we are going to use a third party package or be equipped chase.
So let's get to the terminal and now let's install that package.
Let's create a terminal on an MP and I.
Ain't Jazz finished?
Let's restart our server.
Now the server is running.
The next step is let's require the package.
So if you have been taking my other policies, you can see that I always handle the hash or the password
inside the module.
But for this time around, I just want to do it inside the controller.
So here we go.
Can't be encrypt is equal to required the secret package and that is it be encrypt chips.
That is it.
So how are we going to hash the password?
So here.
First of all, we are going to create a salt from the package as a wait and decrypt dot gen sort and
by convention we use ten.
After that, we are going to hash the password.
So here let's assign the hash password to this variable as hashed password, which is equal to our weight.
And we bring in the big rate package on that we call something hash.
So here I'm going to pass in the plane password and then the salt and that is it.
So after that, we can go ahead and then create the user.
So here we go.
So let's comment here and say that creates user perfect.
So const user is equal to our weight and then the user does not create.
Then you pass in the properties that you want to create.
The first one going to be the full name, the email, and for the password we are going to save the
plane password as hash password.
And if everything goes on, well, we are going to send the response to the user.
So here, let's refactor this one a little bit and let's provide status and start and say success.
You may need this one in the front end.
So I will show you when we get there.
And after that, let's also sign the foo name as user dot foo name and the id too.
You may need this one also in the front end as user.
That is it.
So for the error handling, don't worry, we are going to do that one in this video.
So moment of truth.
But before we continue, we need to tell x player to pass the incoming data.
So by default, exploit doesn't allow that.
So let's go ahead and then do that one.
So let's go ahead and then register a user.
So this is the end point.
So here I'm going to pass in a body.
So let's hit send.
And there we go we go starters 200 which is OC and we have the response great.
But we are not sending the emails, so let's go ahead and send the email also.
So here email is going to be the user dot e mail.
Great.
So let's go ahead and then register with the same email and let's see the message.
And this is the message.
You can see that we have titles, 200 million.
Okay?
That is why I say that this is not an error.
So you're going to find a proper way to handle this kind of situations.
So to check if a user is being saved, let's use the extension that is the MongoDB for visual STD code
and let's click on the connection string and click on refresh.
And this time around we have our database name as income expenses app.
If we open it, you can see we have one collection called Uses and this what I was telling you that
when you compile it is going to utilize this one and convert each one to lowercase as we see here.
So if you open that, we have one document and inside the document this is what we have.
If you open it, you can see this is a data being saved inside MongoDB.
And for the password, we are saving this random string of characters.
That is perfect.
So in the next video, let's go ahead and implement the logic for logging in.
Let's get into that in the next video.