Hated everyone they share.
And in this video, we're going to learn a little bit about the middleware in the Mongoose.
In the last video we saw that how middleware can happen between the express routes, but it can also
happen in the mongoose and tons of other libraries, too.
We're going to combine this with the video where we are going to write code even as well.
We're going to do a solution in this video only because I think this is a short one to explain this
stuff.
Okay, so let's go into the mongoose and you might already be seeing the middleware here as well.
So it is available here and you can read that in Express as well.
It's really the same.
So what the middleware is going to do in the case of Mongoose.
Now remember, Mongoose helps you to write schema as well as save that schema into the MongoDB.
So it gives you mainly two types of middleware which is pre and post.
Now some people you are going to see, they call it as lifecycle hooks, which is also a correct term.
There is no no point in arguing that part.
So we can have that.
Now notice here there are types of middleware which are given to you by Mongoose as well, like validate,
save, remove, update one, delete and initialize as well.
We won't be going in that much of depth right now.
Our focus is only onto the pre and the post.
Yes, you can handle the errors and then synchronous all of that.
But let me show you what happens in the pre one and after that the post one will be self explanatory
to you.
So notice here in the pre one it says that hey, you can define your schema like this dot, dot, dot
means whatever you decide to have first name, last name, whatever you want to have it, you can have
it.
Now I'm saying schema dot pre.
So whatever the schema you are creating, we are simply saying hey now we are saying dot pre and by
the term schema here means that whatever the object that you are creating you can actually inject on
that.
So user dot pre or whatever you are saying inside that we pass on a hook which is save.
This is basically an event like save is obviously a common event.
So before just hitting the save, use this pre so what we can do we can write a function and the only
thing that you have to worry about here is it doesn't accept the arrow function, so you have to use
the function keyword itself, make sure there is a next being included here as well.
And when the things are being done you can use the next, so do this stuff.
So what I have to do this stuff here is simply take the password from the model which is being passed
on to us and we just simply have to encrypt it.
So that's it.
That's all we have to do.
Now, the bigger question is we can do that here, here at this page where the post is happening.
So just after checking the user exist or not, we can actually go ahead and encrypt it here or we can
write it into the module file.
Both of them are correct.
It just the personal perspective here, but the question is how we are going to encrypt it.
That is the question now.
There are so many ways to encrypted it.
We're going to go to NPM JS and you're going to find that a lot of people like to use some of the crypto
packages.
So if I go ahead and say crypto, if I can write that, correct crypto.
So crypto packages are available like crypto JS and all of that, and they can help you to actually
encrypt the data based on what kind of algorithm you are using.
You can pass on a nonce and the message I wouldn't be talking too much and the hash digests and nonce
and all of that don't want to make it a cryptography class.
So this is one way of doing it.
But if you are going to search for B script, which is really an insanely popular package, B script
and B script.
JZ Both we are going to use the script.
JS Because it is built on top of B script.
Now B script is just a library to encrypt, but B script G is actually is more compatible and that's
why you see insane crazy weekly download.
It is kind of designed for these kinds of situations where we are facing.
We are not the only one who wants to encrypt the password.
There are tons of other people.
So how the usage actually works, really simple.
You just install it and just require it, and once you have required it, then you can go ahead and
use as encryption or you can just go ahead and do all the kinds of encryption that we want.
So what we'll be doing is we'll be using a hash onto it.
So let me just try to find it once we have a script and then we are going to use something like not
the hash sink, but we'll be using the direct hash itself and we'll be working on that.
Okay.
So the the biggest interesting thing about it that I wanted to show you is that you can use a big script
dot compare, which is actually used to check the password.
We don't want to just encrypt it.
We later on want to check whether the password has whatever the login feature is.
User will enter the password.
We also want to check that by encrypting it that it matches with the whatever the encrypted password
we have stored in the database and this entire library is built around that so we don't have to worry
too much.
So moving on, we get the idea that how we are going to do it, let's copy that.
Move back up here and close this one and now let's go ahead and install decrypt JS shouldn't take much
of the time and we can start running the dev server up here.
Okay.
How are we going to do that?
Really simple.
We have done it so many times.
Shouldn't be a big deal.
So first let's just go ahead and use B script and that is going to be required.
Four from be correct DJs once this is being required, will go back onto the existing user.
So we have checked whether the user is existing or not and after that all we've got to do is go ahead
and encrypt that.
So once this is all done, we have checked everything.
Now let's go ahead and encrypt the password.
So let's call this one as my encrypt password or whatever you want to call that.
Now how we're going to design this one, that is the most interesting thing.
Now, again, we haven't yet designed this one, so we can actually go ahead and call it as const or
let however you like to go with that.
Now, this is going to take an A wait again because it might take a little bit of time.
Whenever you encrypt that, it goes through a round of algorithms and it might take a little bit time,
not too much as database, but sometimes it takes.
So if you go ahead and say, hey, I want to use the script and you put a dot, it has so many of the
features like compare, compare, sync, decode, get the salt hash and a whole lot of other things.
We're going to use just the hash because we obviously want to create the hash, the password that we
grabbed from request body.
And then you can go ahead and mention that how many iterations you want.
The number here is how many rounds of that algorithm I have to do to encrypt it.
And the more that is better, but don't do it too much because it might take some time.
And sometimes even the storage capacity of particular unit or particular property in the MongoDB might
exceed there.
So ten is usually the good number of length to generate that.
Okay.
This is actually you can see here as well the salt lend to generate the salt to you.
So this is basically it don't want to make it too cryptic up here.
Okay, so now this is all done.
So let's go ahead and construct a user because this is the object that we'll be handling over to Mongoose
that, hey, just go ahead and save this one.
So we're going to go ahead and say this a await and let's go ahead and use this model.
So user and basically let me let me make it a little bit more simpler so that it's easier for you to
understand.
So we're going to take this model and we're going to say that, hey, I want to go ahead and simply
create one.
So this is the command to create a template that command a function.
And inside this you have to pass on an object.
Our object is going to be heavily, not heavily entirely dependent on the user that we have created
here, the model.
So first name, last name, email, password, token.
We'll do we'll do something about it in a minute.
Okay.
So let's go up here.
Sorry.
There we go.
So first we go ahead and say, hey, we have this first name, then we go ahead and say, hey, we have
this last name.
Then we go ahead and simply say, hey, we have this email, and then we simply go ahead and say, Hey,
we also have this password, but this time the password, we have changed it to my encrypted password.
So password will be replaced by the encrypted one and that's it.
Now you will also notice that sometimes people go ahead and change this email to email to lower case
command.
So just me know it's not going to do that to lowercase.
So there we go.
It just a basic JavaScript function to just lowercase the entire email.
Maybe user has entered it in a kind of weird casing and all of that.
So this will actually be responsible for creating that.
Now, further down the road, I told you that anything that happens with the mongoose, it's actually
a dot then dot cat.
So basically it's a promise.
But you can go ahead in this way also.
And another way of handling this situation is actually hold that into a variable just like that.
But since it's a database operation, so we have to go ahead and do a wait totally up to you.
Both methods are same.
Use a dot, then dot catch however you want to go.
That could be much more easier than this.
Okay, so this is all done.
We have saved the user yet, but the job is not done.
Because if I go back up here onto the presentation, I've said that take care of the password.
We have done that.
Now we have to either generate that token or send the success message.
I prefer to not send the success message because that would be easier.
I will choose the little bit of path here to generate token because obviously we haven't talked about
the token yet.
So it's time now.
Let's go ahead and talk about the tokens.