Hey, did everyone they share?
And welcome to another video.
Now, in this video, we are going to learn something different.
And I'm pretty sure most of you haven't talked much onto this one has a big night?
We don't really.
But let's go ahead and work on with that.
So the idea in front of us is somebody who is going to try to save the field, is obviously going to
send us a password in the string format, which will be clear text.
We don't want to store this into a clear text format.
So obviously we want to have an encryption there around.
So how we're going to work with the encryption, pretty simple.
We just need to encrypt that.
First, let's bring and bring up the libraries that will help us to encrypt this password.
We have already worked with that.
So this is B script and we are going to require B script.
JS already worked with that, so shouldn't be a big deal, but what's the big deal is how we are going
to accomplish this task.
So again, once the B script is available to you, make sure you define all of your models before exporting
this.
I cannot stress mode mode onto this one.
I've seen so many people struggling on this path, so please don't do that.
So let's go ahead and call this one as encrypt password before save before save.
Okay.
So how do we actually work on with this one?
This entire functionality is actually rather comparatively easy.
All you got to do is you hook onto something which is a lifecycle event and there are lots of lifecycle
events, the pre and the post actually there are just two pre in the post and we can perform some of
the functionality based on these hooks.
So we're going to say this user schema again, whatever you are naming your schema, you have to hook
on to this one.
This is known as document.
So onto this one, if I go ahead and say, hey, Dot, again, there are lots of fields, virtualize
and a whole bunch of other things.
We are interested in only the pre events or the pre.
That means just before saving this and there can be a lot of things you have to explicitly mention that
what I'm looking for pre is this pre save event.
Now after this we want to run something again.
You cannot use arrow functions in here.
This is kind of a thing with the mongoose just exactly here at this.
You cannot use it.
And since this might take some time, I told you a B script can take a little bit of a second to encrypt
this one.
So it is always a great, great idea to have an async function here.
So this is a sync function.
There we go.
And again, make sure you pass on next to here, because once this is all done, it needs to pass on
the chain to somebody else that, hey, continue whatever you were doing up here.
There we go.
Okay.
How we are going to encrypt this one?
Really simple.
We first say, hey, this dot password.
So this actually can access all these.
We can use this keyword here because we are using just the basic again.
My bad.
This is totally my bad.
Not a narrow function, just that habit again.
So what we can do is we can use this to access any of the property here.
So what do you want to do is that if you are having this password, let's update this password field
by using the script again, we have to use a wait and we'll say, Hey Bay script, just go ahead and
use the hash functionality to encrypt something.
What do you want to encrypt this dot password?
Password and how many rotations of the encryption?
Probably ten is fine enough.
This is a kind of industry standard, but we have introduced a problem here.
Now, what is the problem here is that every single time this document is going to save, it is going
to just go ahead and take the password and is going to encrypt that.
So imagine if you are updating just the rule.
It will take whatever the existing password is there, even the encrypted one, and will again try to
decrypt that.
So again, it will create so much of the mess again by encrypting and encrypting that it's of no good
use for the first time.
Definitely great, but after that it's not great.
So we need to read a little bit of the documentation of Mongoose to actually get away with this problem.
So if I go into the documentation, if you'll read a little bit of the methods, this document means
user schema.
We don't need to write the prototype.
We can directly hook into these methods.
So if I go up here a little bit, you're going to see this on the document here.
There is there are a couple of methods like A's in it.
At the time of initialization, there is a method is modified and there is a method of is new.
So let's have a small brainstorming session.
Which one is going to be good for us, isn't it?
Works at the very first time of whenever you are saving the model.
So at the first time we are saving the password and it works really fine in this case because the user
is giving us plaintext, we encrypt that.
And after that, whenever this comes up, it's it's not going to be working.
That's fine.
But what about when we are working on the forgot password at that time?
Also, somebody is sending us the password field and we need to encrypt that password also.
So there are two times where at least we have to just change the password.
So in this case this is modified field actually works better for us.
So it says it returns true if any of the given path is modified.
LS false if no argument returns, that means true.
If any path is document is modified.
So basically what it's trying to say is whenever a field, that exact field is going to come into the
body or into the salvage path, then only I'm going to run the.
This method, otherwise not.
So let's go ahead and hit on to the Google.
So let's go ahead and work on with the Google.
And one thing really interesting, I wanted to show you that if you'll just search for what is is modified
in Mongoose and you look forward to StackOverflow or tab nine wherever you want to, they'll give you
some examples of exactly this behavior.
And this is not one of a unique problem.
This is kind of a common thing everybody does.
So they give you this.
So user schema dot pre and then save async function next again exactly same.
But what you can do is now you can actually go ahead and say if this dot is modified password field,
return the next what it is saying again, notice here there is an exclamation sign.
So if field if this field is not being modified or being touched, then just go ahead and keep on doing
the things.
Otherwise, if this field is being touched, then only go ahead and encrypt the password.
So this is really the basic one.
So we're going to go ahead and kind of a copy this line and then we're going to be working with that.
So let's go ahead and copy this line from here and go back up here.
And this time we're going to go ahead and save this one.
So again, this is exactly perfect and correct.
There is nothing needs to be modified.
But what I feel like is sometimes this can create a confusion and readability of the code.
Maybe beginners are working on that.
So I'll just add it to this one.
So it makes really clear to them that, hey, if this field is modified again, we are calling this
as password, so no need to change that.
So if the password is modified again it is an exclamation.
So if the password field is not modified, then go ahead and keep on doing whatever you are doing.
Otherwise that means if this field is modified or somehow it is being changed, then go ahead and encrypt
that.
So again, I hope this is very clear.
So the very first time again we are modifying this field, that means we are passing the password.
That means go ahead and encrypt that.
In the case of forgot password, also we are passing on this field.
That means we are modifying this field.
So go ahead and encrypt that.
In all other case where you are just touching the photo or the role or the name or the email, please
don't do anything in the password field.
So this is exactly what we needed and thus we have done a great discussion on this one.
So again, really a basic one.
Again, make sure you pass on this return next.
Otherwise it is not going to work.
Okay, so it seems like all good and all happy and you have now learned about encrypting the password
and some more methods on the mongoose, maybe whatever you are working there in it make more sense.
Whatever the thing you are trying to achieve is modified.
Make more sense.
So again, you now know a little bit more about Mongoose.
Let's go ahead and catch up in the next video.