Let's get started with the user registration.
So back to the controllers and the users.
Let's start with the first one, and that is the register controller.
So in here you're going to have a couple of steps.
Step number one is we are going to check if the user already exists and you are going to check by email.
So let's go ahead and then structure the payload from the red dot body.
So we want the foo name, email, password, and then the other properties.
So let me show you the properties that we need for user registration.
Let me put it side by side here.
All right.
So we need the full name, which is this one.
And then the email also and the password also.
And then the profile image and then the cover image post and comment.
These are optional fields.
And this one, we are going to add them dynamically.
That is the post.
And then the comment.
So now this what we need for now.
So let's collapse this one.
So next step is that let's check if user exists.
So we need to bring in the user model and to have the auto import.
Make sure you save the file that you want to import.
And now back to the file where I want to use that one and I'll make use of user and I have the auto
import.
So now here we go, make use of cost and then user found and is equal to a weight.
And then the user model we are going to use find one.
And in here we want to find the user by email.
And if the user is being found, then it means that this email is being taken by a different user.
So let's go ahead and throw an error here.
We will have a section for custom error handling.
For the meantime, let's throw this one as an error, but indeed it is not.
So let's go ahead and then create the function execution.
And if user found in here, we are going to return this red dot JSON and we are going to provide the
status.
And you say fail.
Data, rather, is going to be as user already exists.
So let's return by bringing in return here.
So below, if it means that this email is not being taken so we can go ahead and then hash the user
password, but we will come back to it later on.
So let's keep this one and let's go ahead and then create the user or register the user.
So here we go.
Const user is equal to a weight and then the user model.
Dot great.
Then you pass in the properties that we want to create.
You want the full name, the email, the password for now, and let's send the data as data and provide
the user.
Oh, right.
So moment of truth.
So let's get back to the tender client and for users, let's pass in the payload.
But we face some well thing here.
But for the meantime, let's add up.
So click on body and let's provide the JSON data.
So the full name, I want to name this one as Emmanuel.
And then the email for this, I would say email at gmail.com and then the password.
I'll use one, two, three, four, five and hit send and I got some error.
Let me check the console and you can see that it cannot structure the property from the request button.
This means that by default Express doesn't pass the incoming payload unless we configure it manually.
I would suggest that this feature might be implemented by default.
That is, to be able to pass the incoming payload, but here is not the case.
So let's add that one quickly here and express make it pretty easy as well.
So before the live version of EXPRESS, unless we install a third party package called Buddy Pass,
but with Express that the current version we have access to that one on the app dot use and then you
bring in express dot JSON and that is all.
So let me comment this one and say pass incoming data.
So with this one, let's register again.
And now you can see that the user is being registered with the start of success and then the data being
sent to us and post empty comments and other properties.
If you check our database, let's right click and refresh and you can see we have a new database called
full Stack block.
And when I check inside, we can see that I have one collection called Users and inside I have one command.
And when I click on that, this is a user that has been registered.
All right.
But the problem here is that we don't need to say the plane password unless we hash it.
So let's see how we are going to hash the user password in the next video.