-: In the last section we did a little bit of refactoring
where we pulled out some request logic
to on an authentication controller
over an authentication js.
Our goal now is to read in a user if one is passed.
So like an email and a password.
We're gonna check to see if a
user account already exists with that given email
cause we don't wanna make any duplicate records.
If one does not exist,
we're gonna go ahead and save the record
and then respond to the request and say,
"Okay, user has been successfully created."
So, that's a lot of steps to put in all at once,
so, let's put in some comments
to help guide us through this process.
First thing we need to do
is see if a user with a given email exists.
So again, we're going to be given
an email and a password as strings.
So we'll see if a user with a given email exists
because we don't want any duplicates.
If a user with email does exist,
return an error.
And then if a user without
or excuse me, with email does not exist.
So in other words, this is like a fresh email,
it's never been used before.
Create
and save user record
and then we will respond to request,
indicating the user was created.
It's kind of a four step process here
but I'm kind of glossing over one very critical step here.
The first step I've listed says,
"See if a user with a given email exists".
And so I'm kind of assuming at this first step right here
that we already have access to that email, right?
But we haven't really discussed
how we pull any information out of this request object.
Remember the request object
is what kind of summarizes the incoming request
or it symbolizes it.
It contains a lot of data about that incoming request.
So we need to figure out
how to pull some data out of this request object.
To pull data outta request, request object
when it is a post request
we can make reference to the req.body property.
So, .body means anything contained within this post request.
Let's see what it looks like in practice.
I'm gonna put a console login just to console log the body,
that's all we're gonna do here.
I'm gonna flip back over to Postman
and I'm gonna change the body,
I've already got some test data in here,
so I'm gonna take it out.
So right now if I send a post request
and I'm not gonna get a response right now
because we aren't responding to the request
in the route handler,
but I should be able to check my terminal
and see, okay, here's my console log right here.
So this is the body object.
I didn't pass any data with my request
in Postman when I made it,
so, right now it's just an empty object.
Let's see what happens, I'm going to cancel the request.
Let's see what happens when we post some data along.
So on the body tab in postman
I'm gonna select the raw option over here
and I'm gonna make sure
that JSON application/JSON is selected as my request type.
Then I'm going to add some JSON in here
and I'm gonna give it an email of "test@example.com"
and a password of how about just "123".
Keep in mind that this is actual JSON
that is expected right here.
So we have to use double quotes
around both the keys and the values,
that is, this is not optional.
We have to have double quotes
around all these different values in here.
Otherwise you're gonna get an error
when the server tries to parse this JSON.
One other thing I wanna say is
I'm sorry I can't really make this any larger,
I know the text is really small here.
Unfortunately, I can't really zoom with postman so,
you guys can, I'm sure you can follow along regardless.
Anyways, we've now gotten email and a password
being passed in the body of our post request.
Let's send this along
and see what we get console logged on the other side.
So now I'll go back over to our server
and I've got req.body is email
and it has the password as well.
So this is how we're gonna get access to our data
in our post request.
This is how we're going to get access to this email
that we referenced right here.
To get access to our email,
you can say const email is req.body.email,
and const password is req.body.password.
And I'm gonna take off the console log up top.
Okay, so that's how we get our
kind of relevant data off this request.
This is the data that we're going to use to figure out
whether or not we should make this new user.
Now, let's go back to that was basically step 0.1.
Let's go back to step one
which is we need to see if a user with a given email exists.
So this means we need to go through
all of our existing database records.
We have to search through every last one
and we have to see if an existing user has already created
or if an existing user already exists
with this exact email right here.
If one does exist, then we wanna throw
or return an error on the request.
So that means that we need some ability
to search through all of our records.
We need some ability to look through
our entire database at this point.
So to look through all of our records,
to search through all of our users,
we're going to use the user model
that we already created using Mongoose.
Let's see what it looks like.
At the top, I'm going to import
our user model.
And then the user model has a method.
This is, remember at this point in time,
we have the user class right here.
So this is the class of users, it's not an instance,
it doesn't represent a single user, it represents all users.
My entire collection of records
of all users that are saved in the database.
So we can search through this collection of users
by using the method, findOne.
FindOne is a method whose first argument
is kind of the search criteria that we want to use.
So, we're going to say find one user
who has an email of email,
email being
the email coming at directly off of the request right here.
After this search is completed
it's going to invoke a callback.
Remember, just about everything in Node
happens with callbacks.
So searching through our list of users,
you know all the users in our database,
it might take some, you know, unknown amount of time.
So, once the search is complete
it's gonna call a callback that we pass.
And the arguments to this callback
are going to be an error object
if there was an error during the search
and what we're going to call an existing user.
Where existing user is basically a user who already exists
with this email right here.
So this is how we're gonna see
if a user with that given email exists.
We just search through all of our users,
we see if someone already has this existing email,
if they do, this argument right here
will be populated with that found user,
the one with that kind of already existing email address.
If they do not exist, if this is a completely fresh account
this value right here will be null instead.
So that's how we're going to tell
whether or not this email is already in use.
Okay, so this is,
I think a good place to take a quick break.
We've got step one complete here.
We can see if a user already has a give email.
We've also got the ability to pull off
an email and password from the request.
In the next section,
let's continue by implementing the next three steps.
I'll see you there.