Now that Mongo D.B. is all set up let's start to work on authentication on our Express API.
We're going to first begin with a quick overview of how we're going to handle the authentication process.
So in this diagram right here I've diagrammed out exactly what's gonna happen when a user tries to sign
up for a new account.
Remember our accounts are going to be based upon email and password authentication so whenever a user
first uses our application they need to first sign up for an account.
When that occurs we're gonna have our phone make a post request to our Express API inside of this post
request we're going to include the email and password that the user wants to use to sign up now.
Emails inside of our application must be unique.
In other words we cannot have two different user accounts with the same email.
So before the Express API receives that email and password and uses it to make a new account the Express
API will first reach out to mongo DV and make sure that nobody has ever signed up with that email before.
If that email is already in use then the Express API will immediately send an error message back to
whoever made this post request if the email is not in use then we'll go ahead and create a new user
account.
So we're gonna take that email and password we're gonna make a new entry inside of Mongo D.B. and reflect
that user's email and password inside there at that point.
We would then consider our user to be logged into our application in order to consider a user to be
logged in.
We're going to provide the user with something called a Jason web token or JWT.
That's also sometimes pronounced as just this Jason web token is an encoded string or essentially a
string that has some information inside of it.
This string is going to prove that the email or smear of the user is the person who provided the email
of test at test dot com.
That Jason web token is going to prove that the user is signed into our application so we will expect
them to provide that Jason web token with any follow up requests to authenticate themselves.
We're going to go into a lot of detail around this Jason web token as we start to implement it.
So for right now we'll just kind of ignore that part instead let's first focus on just making sure that
we can receive some email and password in a post request and then make a query off to our Mongo DB database
to see if anyone has already used that email before.
All right.
So let's flip back over to our code editor.
We're gonna start to write out some code to receive that post request and then issue that query.
So going to flip back over my editor and inside my S.R. C directory I'm going to make a new folder called
Roots then inside there I'm going to make a new file called off routes dot J.S. so inside this file
we're going to write out all of our request handling logic to deal with anything related to authentication
so that might be signing up or signing in or whatever else inside of here we're going to first begin
by requiring in the Express library will then use Express to create something called a router so I'll
say router is express dot router like so a router is essentially a little object that allows us to associate
some number of root handlers with it.
We can then take that router and associate it back with our app object.
We had it created inside of indexed dot J.S. so on this router I'm going to add router dot post slash
sign up like so so we're saying that anytime that someone makes a post request to slash sign up we want
to run that function right there this function will be called with our rec and rez objects.
It's now in the body of this function.
We're going to go through the entire process that we just described so we're gonna see if anyone is
signed up with that email before have some error handling and eventually synthesize that Jason web token
and send it back to the phone.
Right now.
Anytime we get a post request let's just send back another text response so I'll do something like Red
Dot send.
You made a post request so we'll just do a little bit of testing here to make sure we have some ability
to actually send some arbitrary information to our API and then we'll start working on that actual authentication
to make sure that our router is used by our application.
We're going to export it at the bottom to file then back inside of index dot J.S. at the very top.
I will import or require in that router that we just export it so look in the roots directory and find
the roots file and then to use that router with our app that we created right here right after we create
the app.
I'll say app use of roots said essentially associates all the request handlers we added to the router
with our main express application all right.
Now here's the thing we're going to very quickly want to test out this root handler.
Like even right now I want to attempt to make a post request to slash sign up just to get back that
text message and make sure that we associated that router correctly.
The problem however is that we cannot very easily use the browser to do that.
Remember anytime you write out a address inside of your address bar and hit the enter key that makes
a get type request to that address.
So we don't want to make a gate type request instead we want to make a post request.
So to test out this root handler very easily we can use a small third party tool.
So let's take a quick pause right here.
When we come back the next video we're going to install this little third party tool and understand
how we're going to use it to test out our API in general.
So quick pause and we'll try that tool out in the next video.