Let's continue working on our authentication flow in this video.
I want to give you a quick overview of exactly what we're going to have to do inside of our roots.
Sign up dot t s root handler.
We've got a little bit of code inside of here.
But it's mostly all related to doing some of that validation stuff and whatnot.
All right.
So quick diagram just to make sure it's really clear what we have to do.
This is similar to a diagram we looked at previously but I filled in a couple of steps that we have
to take care of inside of that root handler.
So at some point time or react application is going to make a request to our auth service.
The goal this request is going to be to sign up to inside of that request we're going to have some email
and password.
We're then going to take that email and password into a couple of checks with it in order to sign this
user up to our application the person we need to do is check to see if a user has ever signed up with
this email address before because we're going to make sure that all the different users inside of our
app all have unique email addresses.
So if this email address is already in use we need to stop right away and respond with an air and tell
the react application.
Sorry this e-mails and use to check and see if the e mail is already in use.
We need to take a look at all the users we have saved to our Mongo DB database inside of our database.
We're going to have a collection of users remember a collection of users is essentially a big object
that contains a lot of little objects those little objects each of them is going to represent one user
in order to access that collection.
We have to create a mongoose user model.
So we're going to create a user model that's going to govern access to that collection and help us run
queries or create new users or whatever else so when we say that we need to check and see if user at
this email already exists that really means that we're going to run a query on our database and see
if a user with that email is already inside there and we run that query through this Mongoose user model
if there is not already a user with that email then we're going to move on to the next step we never
store passwords in plain text when we receive a password inside the sign of request.
It will be a plain text password.
Rather than storing that plain text password inside of our database we have to hash it ahead of time.
If you are not familiar with password hashing that is OK.
We'll have a quick follow up video that describes the entire hashing and password storage process.
Long story short we're going to do a little bit of processing on that password before we store it inside
after that.
We're going to create a new user and save them to our Mongo DB database.
And again that's going to happen through our Mongoose user model at that point time after we successfully
save the user whoever made the request to us is now considered to be logged in to reflect that we're
going to send back to our ReACT application a cookie or a Jason web token or something that indicates
that this user is now signed in and authenticated.
We'll have a follow up video to also discuss the actual authentication mechanism that we're going to
end up using.
Right now we're going to focus on these first couple steps all these initial steps really rely upon
creating a mongoose user model so we can work with some data inside of our Mongo database.
That's what we're going to really focus on for right now.
We need to create a mongoose user model.
Now I got just unfortunately a little bit of bad news for you.
We are using Mongoose and we are also using typescript it turns out that TypeScript and Mongoose do
not really cooperate all that well.
I'll be totally honest with you.
There is a library out there that gets Mongoose and typescript to work together a little bit more nicely
but I kind of want to limit the number of libraries that we are using for this project.
So we are going to manually get Mongoose and typescript to interact.
It's not really that hard.
Just a little bit of configuration but unfortunately it involves writing out a decent amount of code
that is kind of challenging to understand inside of a single file.
It's going to take a quick pause right here.
We're going to come back the next video and start to write out this Mongoose user model.
I'm going to do my best to make sure that all this code we write is as crystal clear as can be.
The good news here is that with the code they're about to write out we're going to duplicate throughout
all these different services we create about five six seven times.
So even if this first implementation of a model with mongoose is confusing well we're going to get a
lot of practice doing it.
OK.
So quick browser here.
Let's set up that user model with mongoose in the next video.