All right.
And up next, let's set up the validation layer so everything is cool if we're getting the values we're
looking for.
But of course, at the moment, I can skip the name, for example, send over here and I'll still create
the user.
And in order to fix it, we want to navigate to our middleware.
And I strongly suggest to at least try setting it up yourself and hopefully by the end you'll see that
it's actually not as difficult as it seems.
Yes, there is technically a lot of code, but you can clearly see that there's a bunch of repetition
as well.
And effectively I want to come up with a name first.
In my case, I'm going to go with validate, register input and yes, we want to export since again,
we'll set it up as middleware.
So validate register.
Input and that one is equal to our function.
Then we want to pass in our array.
And then one by one, let's go down the list.
Like I said, always, always, always use your model as the guideline.
So for some reason I'm showing you the job model, but actually I want to look at this one.
So we have name, email, password, and we also have last name, location and role.
Now don't worry about the role.
This one will handle a little bit differently, but these ones, at least the basic setup is going to
be almost the same like we have for the job.
We just need to provide the proper names.
So in here, what is the first one?
Well, name?
Correct.
What are the conditions I want to set up?
Well, first of all, it shouldn't be empty.
That's kind of a straightforward.
Right.
So then let's add also with message and we'll say name is required.
Okay, good.
Let's save that.
And then like I said, let's go down the list.
The same is going to be for password, the same for location and also the email.
And yes, once we have that one in place, we'll add some additional features.
So one, two, three and then in here, essentially just change the values.
That's all you have to do.
Email.
Yep, same deal.
Then you want to do the same thing for the password and also for.
Location.
Okay.
That's the straight up setup.
Then when it comes to email, what else we want to check?
Well, two things.
We first want to check whether the.
Email is actually valid email and what's super, super nifty.
The middleware we're using actually has a method for that.
So let's look for the email validation and we just want to chain it.
I also want to go with is email.
So whether it is a valid email and then what message I want to use.
I'm going to say invalid email and format.
And lastly, like I said, we also want to check whether there is already a user with the same email.
So I want to make sure that when user registers only the unique email is used.
So how we can do that?
Well, you can probably already guess that we'll be setting up our own custom one.
It is going to be async since we're.
Going to check in our database.
And remember, when it comes to a value in this callback function, essentially we can access whether
that is the parameter or here or in this case, it's going to be the actual email value.
So the general concept doesn't change.
It's just in this case, of course we'll be accessing the email.
And as far as the logic, well we'll need to grab the user.
So let's navigate up.
We're looking for the user and this is coming from.
User model.
Okay, let's scroll down then Let's look for our custom function.
And as far as the logic, I'm going to go here with const user.
And we're going to go with Await then user.
And in this case, I'm going to go with find one.
So not find by ID not find.
I'm going to go with find one which is going to look, for instance, based on some kind of condition
in my case that is email.
So look for user.
Based on this email, whatever is the value.
And of course, if the user is already there, then I want to throw the error.
Again, this is the case where you can throw the regular JavaScript error because again, this will
be handled over here.
Or you can go with custom one, which is going to be my approach.
So let's set up our condition.
So if the user exists, we want to go with throw new bad request error and we'll just write email already.
Exists like so.
And if the user doesn't exist, we don't need to do anything.
Again, this is a sync function, so we don't need to worry about returning.
True or false?
And then when it comes to password, we can also utilize some.
Cool features from our library.
So again, right after with message, we're going to chain one more method.
And in this case I'm going to go for is length.
As far as the options, I'm going to go with men and I'll set it equal to eight.
Now, when it comes to the actual message, again, let's chain it.
Let's say with message and let's just write.
Password must be at least eight characters long.
And the last thing we want to do is to navigate to our routes.
We're looking for.
Auth router and let's look for our validate and register input.
And now let's place it right before register.
So let's set up the come over here.
Now, of course, if you want, you can clean up the database again, because I can tell you right away
that this is going to fail.
Why?
Well, because we have condition right now for this email.
So once I save notice, name is required, but email already exists.
So of course I can go back over here and add John, but I'll still have this email already exists,
so I'll have to change it to something.
And this is the case that yes, while we're creating our first users, basically before we set up the
login, I will also be removing those users from the database because it's just easier.
But I do want to showcase that.
Of course, if I change it to Peter, then everything is correct.
Then we're able to create the user.
Now, as far as the other errors, I'm not sure whether I'll test all of them, but for example, for
the password, check it out.
I can go here and of course it goes with 400 because the user already exists.
So now let me go to my database.
Let me start from scratch because I don't want to keep changing those emails again.
What's really, really awesome.
We can just navigate over here and we can drop everything.
So in this case, of course, we're looking for users.
Then let me go to my Thunder client and like I said, what I'm trying to showcase is that if the password
is going to be less than eight characters, there's going to be an error.
So let me remove the numbers from the password.
Me send and check it out.
Password must be at least eight characters long.
And I think at the very end of this video, I just want to showcase what happens if the syntax for email
does not match what the library is looking for.
So, for example, if I'm going to go with just Gmail.
Check it out.
Invalid email format.
So hopefully this is clear.
One last thing.
It looks like I forgot to add validation for last name field during the video.
So before moving on to the next task, go back to validate, register input and add one more condition
As far as the logic.
It's exactly the same as for location.
Just change the name and make sure field value is camel case.
So last name.
If you need a reference, please utilize the Readme since it contains the complete code.