Great news.
We're pretty much done with our handling now.
We will add a few small things later, but there will be minor.
So congrats.
If you're still awake and eager to continue on one tiny thing, I want to mention that before we start
hashing the passwords in the error handler middleware instead of logging the message, I'm going to
go with an entire era just so we can see in the console.
If there is an error online, let's keep on scrolling.
I'll remove this line of code.
At this point, it's useless where essentially if there is an error, I will send back the default error
and the message.
Property, of course, is coming from this object over here and I'll start hashing the passwords.
So I'll open up the read me because we have quite a few steps over here.
And then let's just take a look at our problem more at the moment.
My first great, we have our collection for users.
We're saving the users every time the proper values are provided, but I mean, this is unacceptable.
You never, ever, ever want to save passwords, strengths because if your database gets compromised,
van attacker pretty much gets all the values.
And not only that, usually the users use the same password for everything, so a better approach is
actually to hash our passwords.
So before we save it in database, we ask them now.
One thing we need to remember hashing is a one way street.
That's it.
Once you hash it, it's not like you can unnasch it.
You can only compare hash values, which we're going to do when we log in.
So when we register, we hash the password, we serve it in a database.
And then when the user is trying to log in, then we just compare the hash values.
And in order to set it up, we'll use this library be crypt digits.
Again, this is very, very important.
It is Bikram, just not just be crypt, but before we start setting everything up.
I actually want to go over the Mongoose middleware because this is the place where we will hash the
password.
And here I did leave a link.
So you want to follow the link?
Just click on it and you'll navigate here, and we can clearly read the matter, which has a middleware.
It's also called pre and post hooks, and you can think of this as a way for us to do something before
or after we save the document.
And we have a bunch of options out here.
As you can see here, we can validate, save and I'm not going to read all of them.
And the code is going to look something like this where we have the schema in our case, and that is
going to be the user's schema.
And then in this example, they go with pre and unsaved.
So we will do some functionality before we save document in our case, hashing the password.
And then the second argument here is the function.
So we're passing the callback function, and here it's important that we use the good old function keyword
because inside of the function body, we'll use this target document created by our schema in our case
user schema.
So our user document.
And if you lose our function, it's not going to work.
And then there's also a next which we pass on to the next minute or so, just like with express words,
we want to pass it on to the next middleware.
Otherwise basically, we'll just get stuck over here.
Now there is a tiny exception where in Mongoose five, instead of calling next manually, you're going
to use a function that returns a promise basically async away, which is actually going to be our case
because the library that we're going to use the B script just and yes, I'm going to be annoying.
Keep repeating the jazz.
Otherwise, someone might install the other library and then the functionality is not going to work.
But back to my point in the library, the functions we're going to use are async anyway.
So eventually this function will be async.
But I just want to showcase how the next year is going to work.
And also, before we start setting up everything in the user scheme, I do want to mention that when
it comes to register, yes, it's going to be a little bit annoying where at least in my case, I will
keep on removing the users so that way I can just keep on using the John.
I don't want to add 20 users and then each and every time I want a registered user, I need to come
up with those unique.
Not a big deal, but in my case, I always just prefer removing.
Keep in mind that it is only for registering.
And then once we start setting the rest of the functionality, it's not going to be the case.
So what do we want to do now?
Well, we want you well, in my case, what I want is pretty much to remove all the tabs here close
to everything, and then I'm going to be looking for the model user.
And in here, we want to scroll all the way to the bottom.
And in order to set up the middleware, we're going to go with user schema of one pre show before and
in our case, we're going to go with save.
So before we save the document, we want to run some kind of functionality.
And again, you have branch and bunch of options over here, and I'm not going to cover each and every
one of them.
But one important thing that I do want to mention that this will get trigger in our case in two instances.
So in the off controller notice over here, how we create the user using Dart Create and then later
in the update user, what we're going to do, we're going to create this user instance.
Again, the moment probably won't make sense, but eventually we will create that user instance and
then we can always use dart save.
And if that's the case, will also trigger that hook, that middleware.
But if, for example, you go here with user and then find one and update that is not going to trigger
off the hook.
And this is very, very important part, because knowing this will help you avoid chasing a bug that
actually doesn't exist.
So again, very, very important.
Yes, this is a hook that gets called before we say the document, but not every method is going to
trigger it.
And again, I'll talk about this once we get to update user, but this is something that I wanted to
mention right from the get go.
So let's say this one online back in the user, why do we want to do we want to pass in the callback
function?
And again, we want to go with good old function here.
So the function with the function keyword and for the time being, here's what I want to do.
I want to go with log and let's just log this dot and password and a quick update, as you'll see in
a second.
It looks like, at least in my case, the request is being passed on to the next model anyway.
Basically, without calling next part, if it's not the case in your setup, remember about the next
parameter and the fact that you need to invoke it.
With that said, in next video, we will make this function async anyway.
And at that point, like they're mentioned and docked, it will work regardless.
So let me navigate back to my post, man.
And in that case, I'm going to go with John, and I just want to see whether I'm getting back that
value.
And as you can see, I am.
So I get back to John over here.
And then when it comes to the console, I can clearly see the secret.
And once we know how the middleware works, now let's implement harsh password functionality.