Hated everyone that they share.
And welcome back to our user model and if you need to have a little bit more brainstorming session on
this one.
So in the previous one, we saw that how this user model is going to actually work.
We have name, email, password, photo role and created that, but we just discussed that how we can
add more functionality into this, which is actually the forgot password and all of that.
So let's go ahead and why not to add this one as well?
So somebody wants to reset the password or forgot the password.
So we're going to go ahead and call this one as simply reset password, pass word token.
So this is going to be the field where a token generator will generate a token and we'll save it up
here in the database.
Now, similar to this, we will have a reset password.
Expiry.
Now, what this field is going to do, this field is going to be responsible to verify that whether
the token has expired or not.
Now, similarly, you can add up a simple kind of a sign up a verification token and sign up verification
expiry so that we can have these fields as well.
Now, a couple of interesting thing comes up here as well, that if we are going to have a user, obviously
user will have a token JWT token as well to verify and all of doing that.
Now the big question here is that do you want to create a separate functionality of them through the
controller or do you want to add some methods right into the model itself?
Now, a common practice is that in most of the applications, some of the methods are injected right
into the models, which are really the common ones.
So let me just go ahead and add these ones and show you that.
Yeah, these are the common models that we go ahead and place it up there.
I think this is not a good color.
We'll just choose something else.
Let's go ahead and use the purple one.
Okay.
So what we're going to do in this one, we're going to go ahead and first work on with the JWT token.
So why this field here?
Now, as soon as anybody is going to register or maybe in your case you don't want to give a token on
registration, you want to give it token on login only.
But the thing is that no matter what happens, this token creation is inevitable.
This is going to happen.
So rather, if we create a kind of a function right into this model, then we will be able to just generate
a token right out of the box by saying, just like we will be able to access user name, user dot email,
we'll be able to say user dot, get JWT token, and it will make our life so much easier.
Now another thing that we are going to work on is kind of a pre hook, so we're going to inject a pre
hook.
Now you might be wondering why we are injecting a pre hook.
Now we are going to be taking password from the user into a very clear text format.
But do we want to store them in a clear text format?
Absolutely not.
So rather than we previously saw that how we can do this kind of encryption of password in the controller
itself.
But since this is obvious thing, why don't you just inject it up here so that we don't have to do anything?
We just pass on the clear text format to the model itself.
And before saving that, that means pre saving the model itself.
The hook is going to come in and that will collect the password and will encrypt it.
So that would be so much easier for us in the future.
Now obviously we need to have a compare password as well because it's not just the responsibility that
we need to encrypt the password.
We need to also get a method that, hey, there is an encrypted password in the database, so we need
to compare that.
So we'll be passing on the same secret, same number of rotation that we'll be doing, just like we
did that in the past.
And we'll be adding this method also in the in the roll itself now.
Okay, so this is all good.
But also further down the road, we will also create a method which will be get a get for got password
token or any similar name like that.
So why we are creating this one up here now again, this is going to be inevitable if you are creating
a field of reset password, token reset password, then we need to create a reset password token as
well.
In fact, we should call it one as a get reset password token to make it things like more in the flow.
So this is going to be get reset password token.
Now again, if I have something like get sign up verification token, I would love to inject it right
here into the model so that I can use this one.
So not everything is done inside the model as well, but these are a few things or few features that
you are going to see everywhere in the big corporate organization that they love to inject this one.
Now, in case you remember, we have already worked through with the Mongoose model and their documentation
that before exporting the model you need to place all these fields just about that.
So please make sure that you also remember that.
Okay, so this is all the theory that I wanted to talk and discuss about this.
Now our all models and fields are all ready.
In case you have understood, just like 40% of it, that is more than enough you want.
I don't expect you to just understand everything here.
I expect you to understand the things when we are actually writing the code and designing the things
there.
So go ahead and don't worry too much about it.
You will understand each and everything up here that what are these pre hooks and compare password and
all of that.
When we are designing these models, code actually makes things much more clearer.
So let's go ahead and catch up in the next video.