We were just able to successfully save a user to our users collection for the very first time.
So now we need to kind of go back a little bit and make sure that we do some kind of test to make sure
that no one's ever signed up but they give an email before.
Before we add in any code for that let's just tried to create a another user account with the same email
as the first one and see what happens right now.
So back inside of postmen I'm going to make sure I've got the same email and password that I had inside
of here from my first user that I created and I'll send the request when I send the request.
It looks like it's just kind of hanging here and not resolving at all.
So let's try flipping over to our terminal and see what happens back over inside my terminal.
I'll immediately see a giant error message here.
The error message says that we've got a duplicate key in collection users.
So remember when we put together our user model back inside of our user J.S. file right here when we
defined that email a property right here we added on the flag of unique true by adding on unique true
mongoose is going to modify our Mongo DB collection of users and it's going to tell Mongo D.B. that
every user must have a unique email so we don't have to do any additional steps to make sure that a
given email is not in use mongoose is going to already do that for us automatically.
And there's one other scenario that we need to be aware of around this kind of error handling stuff
as well.
The other scenario I want to point out very quickly is that if we cancel it's running requests and if
we remove the email and password altogether from the request we are making and then send that request
as well.
We once again are going to have a hanging request so we can flip back over to our terminal once again
see another giant error message here and kind of tucked away inside this error message.
It says user validation failed.
We have to have a email and we have to have a password as well.
And once again we're getting those error messages because we put in a required flag of true on our user
schema.
So it turns out that by defining this user schema and putting into unique and required properties inside
of here mongoose is already doing a lot of validation for us.
So all we have to do is somehow catch this error that mongoose is throwing when we try to save our user
and relay that error message back to whoever making is a request a request to our API so to do so we'll
go back over to author routes when we attempt to save our user to the database.
That is when our validation is occurring.
So that's when mongoose is going to check to make sure that we provided an email and a password and
when Mongoose reaches out to mongo DV and attempts to save the user that's when the check to see if
you email is unique is going to occur as well.
So if you want to catch any error that is thrown during that process we can wrap this entire block right
here with a tri catch statement so I'll put those three lines of code inside of a try statement close
it off with a catch and inside of this catch statement right here we can catch any error that's thrown
during that safe process yet either a unique email validation error or the lack of an email or password.
So then inside of here we can send that error message back to the user and tell them exactly what went
wrong when they tried to sign up.
So inside of here we could do a red dot status.
So we're going to set the age TTP status code on the response I'll put in a four twenty two which indicates
that the user sent us some invalid data to create a new user then I'll put a send inside of here and
send back an actual distinct error message.
In this case we don't really know what went wrong either the user provided a duplicate email or they
didn't provide the email or password.
So for right now we'll just send back ja dot message and that's going to be essentially a message that
is produced by Mongoose for us automatically.
I'm also going to put a return inside there as well to make sure that if I ever add in any code after
this try catch statement I won't attempt to execute that code because something already went wrong up
here so I don't want to try doing anything else OK.
Let's save this and try submitting some invalid requests again so I'll save that file.
I'll go back over the post man.
I'm first going to try making a request without anybody inside the request or I should be more be more
exact without an email or password.
Now when I send the request I immediately get a response and that response says user validation failed
because you have to have an email and a password.
So now if I enter in an email and a password and I'm going to put in the same email that I put in previously.
So this is a email that's already in use.
I'll send that an LLC duplicate Eric key in collection.
We've already made use this email before.
Now I want to point out that the error messages that we're sending back to ever makes this request are
not very user friendly.
In other words if we just put this error message directly in front of a user they would probably have
no idea what we're talking about.
Nonetheless we're just going to leave it like this for right now because well once again we're kind
of focused on the react native side of things.
I just want to point out that perhaps these error messages are not that great for use right now.
OK.
So we've now got some better air handling inside of here.
And we also kind of automatically implemented that check to make sure that the email of test at test
dot com or whatever it is is not in use.
So now if a user gets past that check and if they also provide a email and password now it's time for
us to attempt to create a Jason web token and send that back to our user.
Remember they're gonna use this token as a means to authenticate themselves on future requests.
So now that we've got this error handling in let's take another pause when we come back the next section
we're gonna have a quick discussion and understand what a Jason web token is.
And then whenever user signs in from our sign up Handler right here we're going to make sure that we
create this Jason web token and send it back.
So we'll stay clear that in just a moment.