-: So this section that we're gonna start right now
we're gonna start talking about some of the tricky stuff.
So this is where some
of the more challenging stuff is gonna start to come
into play with regards to authentication.
Let's take a look at a diagram to kind of look
at where we are and where we're headed.
So this is a diagram
of all the different libraries that we're going to
eventually use inside this application.
And so far we've basically used everything on the left half.
So we've got our http module
from Node that's doing the low level request handling.
We've got express for handling responses
or for handling routing and whatnot.
We've got Morgan for logging.
We've got our body parser
for parsing http requests to get J s O out them.
And now we're gonna start moving onto the right hand side.
So the next thing we're gonna be working
on is our connection to our database
which is gonna be MongoDB.
MongoDB is not something that we're gonna work
with just vanilla or just by itself, you know
hands on bare metal.
We're gonna be working
with a library that's gonna be sitting
in between us and MongoDB called Mongoose.
So Mongoose is what's called an ORM
and is a library that is used to interface
with a database in some fashion.
So we don't have to deal
with the very direct manipulating the database and whatnot.
All that behavior is gonna be abstracted
out to Mongoose for us.
We already installed Mongoose in an earlier section
so we've already got Mongoose installed in our project.
All we need to do now is actually make use of it.
So to make use of Mongoose, the first thing we're
gonna do is we're gonna create what's called a user model.
So this is a model
like a data model that represents a user most users
and is certainly the one
in our application is going to have two attributes
or two properties and email and a password.
We need to actually tell Mongoose about these two
properties that we expect this user model to have.
So in this section we're going to create a new user model.
We're going to feed it to Mongoose
and we're going to make sure
that this model has a email field and a password.
Okay, So let's go ahead
and give this a shot.
Back over in our project directory
I'm gonna create a new folder called models.
Right now we're only gonna have the user model
but if I was expanding this project out in the future
I would probably add in, you know
additional files in here for the other models as well.
So like we mentioned, we only have one model right now.
It's gonna be the user model.
And so I'm gonna create a new file
inside the models directory called user dot js.
So inside of this file is gonna be our local definition
of exactly what a user is, and we're creating this
so we can tell mongoose, hey, here's what a model is.
It has an email, it has a a password, you know
handle this for me, handle this.
This is where we get to set up some level instructions
for mongoose that describes our application.
So as you might imagine, we're going to need to
require mongoose up at the top of this file.
We'll write cons to mongoose, require mongoose
And then we're gonna pull one property
off of Mongoose called Schema with a capital S.
So right mongoose dot schema.
So here's already, you know
things are starting to get a little bit interesting
right? Well, I said we needed Mongoose
but what is this schema thing?
So schema is what we use to tell mongoose
about the very particular fields
that our model is going to have.
So the first thing we're gonna do
in this file is we're going to define our model.
We're gonna say, Hey, here's the model.
It's a user model.
It has a property email, it has a property password.
We're then going to create the model class
and then we're going to export the model
so other files inside of our application can make use of it.
So let's get started with defining our model first.
To create a model, we'll create first a schema.
So we'll say constant user schema is a new schema
and we're gonna pass it in object.
And inside of this object right here, we're going to
pass the properties that this model is going to have.
So as we discussed previously
this model is going to have an email
and it's gonna have a password.
Now the other thing that's really relevant
for these fields is the type of data that they are
you know, is it a string, is it a number, is it an object?
What is it exactly?
In both cases, both the email and the password are going to
be of type string.
So I'm gonna put email, string and password string.
When I say string right here, this is literally a reference
to the JavaScript string.
So this is not a variable that we have to import in
or something like this or something like that.
It is literally JavaScript string.
Now I've defined two properties on our schema.
One is email, one is password, and they're both
of type string.
But I wanna like, I wanna, before we go on, I wanna think
about this for a little bit
right? I wanna think about our application
In just about, I'm not gonna say just about
I'm gonna say literally every application I've ever used
whatever is used as kind of like the username
or the identifying piece of information of like, you know
who am I?
Basically this email property or that identifying piece
of information always has to be unique.
So in other words, if I create one username
or one user account with an email of steven@gmail.com
I can't log out and then create immediately another email
another account using email, steven at email at gmail.com.
In other words, we want to enforce uniqueness on this email.
Whenever a user is created with a particular email
no other E, no other user should ever be able to
use that email again, basically for any account
one email cannot be reused again inside of our application.
So to enforce this uniqueness, we're going to pass
an additional property to this email field right here.
Now, to pass an additional property, instead of
passing just string as this value, we're going
to assign it an object instead.
So I'm gonna wrap this in an object and I'm gonna
say that it is of type string.
So at this point, nothing has changed.
We're still passing some configuration for the
email field and it is of type string.
To make sure that this field is always gonna be unique.
We just pass on an additional property of unique
true.
So now whenever an email or a user model is created
and someone tries to save it, it is going to tell MongoDB
Hey, here's a user model.
They have an email.
Before you save this model, before you save it
to the database, make sure that the email is unique.
Make sure that there are no other users already created
with this same email.
If there are throw and air, that's what this unique check
right here does.
Now, one interesting little side effect of this is
that MongoDB does not enforce case in strings when
it's doing this unique check.
So in other words, if someone tries to register with
like steven@gmail.com
and then another person tried to use
Stephen@gmail.com with all capitals
Mongo would think that these are two unique emails.
So to make sure that these always get saved
with lowercase characters to avoid this kind
of uniqueness complication right here
we can add another requirement to it
to this option that just says lowercase true.
And that means whenever any string is saved to the database
it will first be turned into lowercase
in which case this would then fail the uniqueness check.
Okay.
So this is looking good again, we created a user model.
It has two fields, email and password.
They're both of type string
and the email and forces some uniqueness on it.
Let's go ahead and save this, and we're almost done here.
So let's just go another minute
or two.
To create the model class.
We need to actually make use of Mongoose.
So, so far we've just created a schema
and we assigned it to a local variable.
Now to actually create the model class
which is what we're going to use to actually
create new users, we'll make a new variable model.
And then we'll say Mongoose model user
and user schema.
Like so.
This basically loads the schema into mongoose
and it tells mongoose, Hey, there's a new schema here, it's
about a user and it corresponds to a collection named user.
So we add the model as schema
to mongoose and we get back our model right here.
Now I'm gonna, you know
how about let's rename this variable right here.
I'm gonna rename it to model class
just to really indicate what it is.
This is a model class, which means it represents kind of
like all users, not a particular user.
It is a class of users.
Now the last thing we have to do is export the model.
And this part is a little bit more straightforward.
Again, in Node.
We are not using the export keyword here yet
cause we don't have support for it.
So instead we'll just use module dot exports
model class.
Like so.
Cool. So this looks great right here.
We've defined our user schema.
We created a model class out of it
and then we exported it for use in other files.
This looks good.
Let's give a shot
at making use of it inside the next section.