Let's get started working on our sign up root handler and make sure first that a user can sign up to
our application.
No we'll worry about allowing them to sign and sign out and fetch the current user.
So to get started I'm going to find my sign up router.
So here it is right here.
We've got the appropriate method and the appropriate route on the inside of your.
We need to figure out exactly how we sign up a user.
Well the first thing we probably need to think about is the information that needs to be provided to
this root handler.
In this diagram I said that we need to receive some kind of email and password on the incoming request
or more specifically inside the body of the request.
So let's try to just pull that information out back inside the sign up root handler.
I'm going to try to pull email and password out of req dot BODY LIKE SO know right away.
I want you to just understand that we've kind of made a really big assumption here.
We are assuming 100 percent that there's going to be an email and password on the body.
We're not.
Maybe we're not 100 percent assuming that just yet.
Well let's imagine that we just took that information and then used it to create some new user right
here.
So if you said something like make a new user of some kind with an email and password who knows what
user actually looks like that's just some imaginary code.
But we're kind of assuming that email and password are actually defined and actually contain an email
and a password.
It is entirely possible that a user might send us some kind of malicious request or even just a faulty
request one that doesn't include an email or a password so I don't want to build anything in this application.
As I mentioned many times taking that many shortcuts on make sure that a lot of the stuff is as production
as possible.
So I don't really want to have any code that's just going to assume that a user is providing an email
and password as expected instead.
I want to put in some validation attempt inside of here.
I want to make sure that email and password are actually defined.
We could do that by adding in a little check and saying if there is no email or if type of email is
not equal to a string then we can do a red dot status of or 100 should be status like so and a sense
of maybe provide a valid email or something like that.
Naturally write out the code you see right here would get very tedious very quickly.
So rather than doing any kind of manual validation we're going to rely upon an outside library instead.
Let me show you the library we're going to use at my terminal.
I'm going to navigate over to NPM jazz icon
once here.
I'm going to do a search for Express dash validator Well then find the first results and then once here
I'm going to find a link to the documentation for this module if you've never used to express validate
or before.
It's a very simple little module that's going to make sure that a user provides the appropriate information
on incoming requests.
It's going to do some automatic validation on this information.
It can also sound sanitize that information as well validation is the process of making sure that information
has the correct structure and form whereas Sanitization is going to change that information in some
way good might try to remove certain types of characters or stuff like that if you want to you can read
through the documentation of express validator but rather than going through all that I'm going to show
you directly how to use it.
Naturally the first we have to do is install it into our project using NPM.
So we're going to go back over to our terminal I'm going to change into my off directory and then inside
of here we're gonna do an end install of express dash validator like so so let that do its thing.
Now when we install that module that of course makes a change to a package not just on file we are now
listing out express validator as a dependency to our project scaffold is watching all the files inside
of our project directory.
And so as soon as it sees a change to a non sync file in our package that Jason File is not sync that's
going to decide to automatically rebuild our image and then update the associated deployment.
So as soon as I ran that command you'll see that scaffold detected the change it decided to rebuild
the image after the image was built scaffold then updated our deployments and we can now see the updated
right here.
Now naturally the port itself is emitting the exact same logs as before.
We did not make a change to the actual log in information but the thing definitely did restart.
Now we should have expressed a validator instead of our project so to use it I'm gonna go backwards
my sign up dot t yes file at the very top.
We are going to import body from Express dash validator there's a couple of different ways to use Express
validator.
We're going to be using this body thing.
This is a function that's going to check the body of the incoming request.
We can also use Express validator to explicitly check for say parameters like let's say if we had an
I.D. parameter on here for some reason we could use Express validate or to do some validation around
that I.D. parameter.
It can also handle validation of query strings as well.
But in this case we don't want to do a validation of parameters or query strings.
We want to do a validation of the body of the request to make use this body thing.
We're going to apply it as a middleware.
So right before our callback function right there I'm going to add in a coma or then put in an array
and we're gonna list out our validation steps inside of disarray
so I'm gonna put inside of your body look for the proper email property.
So this means in the body the incoming requests look for the email property.
We're then going to chain on a couple of checks to run on that property a chain on dot is email.
This is a validation function that's built directly into express validator or technically a dependent
package of it.
This can make sure that this email thing has the structure of an email.
We can then optionally me append on a error message.
If there is something wrong with the email property we can do that by writing out dot with message and
we could say something like email must be valid.
Now if anything is wrong with this email if it doesn't have the structure of an email or no email is
provided at all then we're going to automatically give ourselves or produce some kind of error message
that's going to say email must be provided and it'll be up to you and I to figure out how to send that
back over to the user so we can do the same thing for the password that we expect to receive in this
thing as well just as a reminder we expect to receive an email and a password
they'll do a body password and then for this we will do a trim that is a sanitization step that's going
to make sure that there are no leading or trailing spaces on the password all then make sure that the
length of this thing is at least four and at most 20 and then I'll attach a message in case the password
is not present or doesn't have that supplied length we'll say something like password must be between
four and 20 characters
and that's it.
That's how we add in some validation.
Well almost how he added in stuff do when that subtle step you'll notice that as soon as I added in
that array for these two Middleware is by the way we got some errors coming out of Iraq and raised right
here.
So typescript is getting a little bit angry as telling us that it doesn't know what the type of wreck
is or rez we could pick that up by adding in some type annotations to those two arguments to do so I'll
go back up to the top.
I going to find where we import Express and I'm going to import in these types of requests and response
and then I will annotate Rick and rez with those two types that will say req is going to be up type
requests and rez will be of type response and then those errors go away OK.
Well we definitely added in some code right here to make sure that the user is providing email and a
password but at no point time do you appear to actually be consuming this information.
In other words we never seem to take any actual errors that might be coming out this validation step
in sending the back to the user.
So let's figure out how we do that in the next video.