-: In the last section we added our logic to see
if a user is already making use of the given email address.
So, remember this is our signup route.
So, we don't want to allow people to keep signing up
with the same email address.
We wanna make sure that each user has
a very unique email address.
So, we implemented step one and now we're onto step two.
We're gonna say if we did find a user in our database
if this existingUser is populated right here
if this has a value.
We want to return an error.
We're also going to handle the case in which
the search itself through an error.
So, this err object right here would be populated
with an error if say
a database to the connection did not exist.
So, let's first handle that case.
We'll say if there was an err return next err.
Again, this would handle the case in which
connection to the database just failed.
And now we'll handle the case in which
there is already an existingUser.
So, I'm gonna move this comment up
just to make sure or just to make clear
that it corresponds with this If statement.
So, if a user with this email does exist
that would be indicated by the presence of
this existingUser.
We're going to return an error on the request.
So, return and error on our request
we can just write return res dot status.
Dot status is going to set the http code on our request
or on the response.
We're gonna set it equal to 422
which means
Unprocessable Entity.
In other words we couldn't process this.
You, the data you supplied was bad.
You gave us an email
that corresponds to a user that already exists.
So, we'll set the status to 422
and then we will send the response off with an error of
"Email
is in use".
Okay.
So, that's gonna handle the case
in which an email does exist.
Let's now handle the case in which a email
or a user with this existing
with this email does not exist.
So, we want to now create and save this user record.
I'm gonna first move this comment up into a callback.
Let's also move this comment up.
Cool.
So again, if a user with the email does not exist
we want to create and save a new user record.
So, to make a new user record
we're going to use the new keyword on our User class.
That's gonna make a new instance of our user model.
So, we'll say our new user
is gonna be a new User.
And then we pass into this an object
with the attributes that we want this user to have
specifically the email and the password we want it to have.
Now, if you recall we already pulled off
the email and and password off the request up here.
So, we can just make direct use of
the email and password inside of this new User constructor.
So, I'll say email is email and password is password.
So, this creates my new user.
But very importantly it only creates it
it does not save the user.
It just creates this in memory right now.
So, this is just an in memory representation of this user.
To actually save this user to our database
we need to call
user dot save.
So, this will save the record to the database.
Now, as you might imagine this is another operation.
Any time that we start talking to the database
we're talking about something
that's gonna take some amount of time.
It's gonna take some number of milliseconds
to go off and save this user.
And so, if we want to know when this user is saved
we need to pass a callback.
So, we'll pass a callback in.
Callback as usual is being called with an err object
if it failed to save.
So, if the user failed to save
we'll say if err
return next err.
Otherwise, at this point in time
we have successfully saved our user.
We can now respond to the request indicating
that the user was created.
And we'll do that by saying res dot json.
Let's just send back the entire user for right now.
Let's just verify that the user is actually getting saved.
Let's see what the actual saved user model looks like.
Okay, so I'm gonna save this file.
I'm gonna flip over my terminal
make sure I don't have any typos in here.
Everything looks good.
So, let's go back over to Postman.
I've got a post request
going to localhost3090 with route signup.
I've got a body set with an email and a password.
Let's give this a shot.
I'm gonna send and my response is looking pretty good here.
I got back from my database an email, a password, and an ID.
That was
this record was saved with.
So, whenever we save a record in our database
it automatically gets this ID property assigned.
So, this is looking good.
Let's make another request now
and see what happens when we try to create another user
with the same exact email.
So I'm gonna send
and I get the error "Email is in use".
Perfect.
Just what we wanted.
Email is in use.
Cool.
So, this looks great right here.
I like it a lot.
As a quick wrap up
let's run through the comments one more time.
In our request handler
we pulled off an email and password off the body.
We saw if a user with the
given email address already exists.
If a user did exist already we sent an error back.
Otherwise, we created a new user model and saved it.
We then responded with some json
that contained the user model.
Now, we're definitely not gonna want to send back
the whole user model.
You know we don't wanna send back
to the user whatever their password was.
You know, I can now change this email to say
test one create a new user
and we're sending the password back in plain text.
We definitely don't wanna do that.
So, let's change the response right here to just be success.
True.
Let's try making a new email now
and we get success, true.
Perfect.
So, this is looking pretty good.
Let's continue on in the next section.