- [Instructor] Any shop needs customers or users and also
our shop comes with the concept of a user
and we need it later to implement things
like a shopping basket or some order management.
For that, I created this Manage User section here,
and as you see here, our users pretty simple.
They just have an email address and a password.
So let's try to fill out this form,
john@acme.com and you get some password and hit Submit.
And as we see here
we have still some work to do in our code.
So let's switch to visual studio code
and there I open Shopper, Server, Routes,
Admin, User, Index.js and as before
we have the whole logic here commented out
because it contains the database logic
and we will now enable all these routes.
Let's start with sketch route in line six.
So you see that it takes an optional user ID.
If no user ID is given, this route will return
all users in the database and with the user ID
it will return a particular user.
Let's remove the comments there
and this return statement in the beginning,
also this leftover in the end.
And we have to add a next argument here on top.
You also see that we are missing the user service here
but we will bring it in, in a second.
Let's scroll down further.
Here we have a post drought that is used
to either update or create a new user.
And I enable that as well.
And this lint helps a lot here as you see,
because here now we see that we don't need
this next argument anymore.
And here in line 67, we have delete routes.
So given a user ID, we can delete a user.
We can remove the next here and in line 88
that's very specific for our demo application.
This route is called impersonate.
So this is used to become a user.
So to set the current session to a given user ID
we will need that later to test things like putting things
into the shopping basket or placing an order for a user.
So let's enable that as well.
And we can remove the next route.
And as I said before, if we scroll up
we see that user service is missing
and I will just require it.
Now, here after we require express ed,
const, user, service, equals require.
We have to step a few levels up
and to gain services we have to use a service.
And now the app crashes
and this has a reason and we will fix it in a second.
I will now head over into the user service.
And if you don't know that you can just press control
or comment on a Mac and over, over such file name
in a requirement, then jump right into the file.
So now we are in our user service
and this is very similar to the item service.
Just with one little difference here in line 17
we have the update method and in this update method now
we have first fetching a user by user ID.
Then we set the email address
because it's mandatory anyways.
And then in line 23
we check if a password was provided, because for instance
if you want to update the email address
but you don't want to change the password
you can leave the password empty.
And we want to make sure
that we only set this password if it is actually provided
so that we don't set an empty password on there.
And mongoose also keeps track of
if a field was actually modified or not.
And this becomes important.
Now when we look into our model, so I scroll up and over
over the require line and click on it.
And now I'm in the model
and here you also see why the app is crashing.
We are missing a module called bcrypt.
So let's install this module now,
npm install bcrypt, and I will
of course explain what it is in a bit.
Let's now just make sure
that the application is working again
and after it's installed, I can again run in npm run dev.
And now we have, again
a working application let's first look at the schema.
So our user is represented
by an email address and the password
the email address is a string it's required.
It isn't unique index.
This means the email address can only exist once
in this collection.
And we also have a modifier here.
We set lower case to true.
This means that mongoose will automatically
convert what we set on this field to lowercase
which makes totally sense for an email address.
The same applies for trim.
So trim will remove trailing spaces makes also sense
for email address
because there cannot be spaces in an email address.
We also have this field password.
It's a string it's required and we also trim it.
In line 16 to see that there is a timestamps
to true so that we get this created at an updated
at times 10th.
When we scroll down a little bit down to line 23
we see that we have here.
A method uses Schema.pre with the argument save.
This is a so-called hook.
And this is a function that will execute it before
a document is actually saved
we can use it to modify our data before it's saved.
We need this because we don't want
to store a plain text password into the database.
And this pre save make sure that every time a password
is provided it is passed to this general hash function
in line 18 that uses the bcrip hash function
to create a hashed representation of our password.
And as password encryption is a rather complex topic.
I will tell you more about this in the next video.
Just let's scroll down a little bit
and let's look into line 38
where we define an instance method, compare password.
This means that if I, for instance, use
find to find a user this user object
will then have a method compare password.
And this compare password method takes an argument.
You see it here, it's the candidate password
and it uses them bcrypt compare to check
if the provided password matches the password
in the database.
And this is mandatory when you use encrypted
or hashed passwords, but let's talk
about this in the next video.