Hey, did everyone share?
And welcome to another video of this section.
I hope you are enjoying this course.
If you are enjoying it even a tiny bit, let me know.
And then Instagram share a story, click a photo or something.
I would be really, really happy to see your feedback and it motivates me to create more awesome courses.
So we have applied a lot from this strategy.
We have seen that how we can register and check already registered users.
Now the problem in front of us is to take care of the password.
I can directly introduce you what the solution is, but I'm not going to do it like that.
I will introduce you to all the possible use case scenario that can happen so that whenever you are
designing any back end, you know what?
All situation goes in and you find out that yes, all of them situations are absolutely correct.
You can choose any path, so it is enough.
Now we cannot avoid one thing which is getting introduced to the middleware.
A lot of people believe that middleware is a concept of express JS or some routing concept, which is
not true.
Yes, it is being used quite a lot in the routing and express, but it is equally used in mongoose,
other libraries, other frameworks and in other languages as well.
Like Python and Java.
This concept is whole in general.
So that's why a lot of people actually adopted a lot of frameworks and libraries adapted by no mean.
This doesn't mean that this is only a concept to JavaScript or only to Node.js.
It is not like that.
So first, let's understand what is the middleware?
Now, middleware is something which happens in between in the middle.
Yes, I know this is super easy, but let's elaborate it a little bit so that you understand it permanently
for the fullest of all.
Now in the middleware, let's go with a very simple example.
In fact, a couple of them, let's just say you are in the office and you want to go to home.
But this is a weekend and you don't want to directly go to home.
You are going to go first to the bakery.
You decided that I'll buy myself a pizza, probably a soft drink, and then I'll go to the home.
So what you did there, you did something in between.
And that's the whole idea of having a concept of middleware.
You do something in between now you actually pass on after that or not, that's up to you.
And the second example will give you more idea of what middleware possibly can do.
You are in the office and this is a working hour probably Monday morning.
And this is you are working into a marketing job or a field job.
And in that you have to go through with couple of vendors.
Now, these vendors are somebody who takes some material or raw material from your organization, and
this is your day where you want to collect some money.
So you go to the vendor and this vendor says, Hey, you take a check from us and you go to the bank
to deposit that check.
But there is also a possibility saying that this vendor doesn't give you a check, but rather gives
you a cash.
So in that case, you simply go ahead, go to your collection office and deposit that check so you can
see in this case, the middle weight is heavily influencing you what you really should do next up.
And if the vendor is not giving you anything, you should not proceed further.
This is one way example, but this can also happen.
So again, moving on to this same scenario, what it can happen, let's go ahead and take it to the
technical side of it.
Now, this is some kind of check or functionality that is injected in between what?
In between?
Yes, it can happen in the root as just we saw, but it can happen in the process of doing something
like, for example, you want to save something into the database just before the save happen, do something
or just after the save happen, do something.
They are also middleware.
So it doesn't mean that the middleware should happen only in between.
It can happen just after that as well.
Sometimes this can also be called as a life cycle event or life cycle hooks as well.
Totally fine as a definition as long as you get the idea and what the concept in between.
We are not in the phase that we should be fighting about what we are calling it.
That's not what we are going to do doing up here.
Now, since we are talking more about the Node.js friendly environment and especially Express and everything,
you should also know about the next keyword.
Now this keyword is really important if you're going to be writing any kind of middleware at all, let's
go ahead and see what this keyword is responsible and how it is going to inject its functionality in
the perspective of the web development.
So let's just say a request comes in to you or your application and you decide that, hey, I am not
going to allow this request is, by the way, to access the dashboard, probably an admin dashboard.
So you have designed multiple functionality in between.
You have said that, hey, this is a functionality or feature to check it whether you are logged in
or not.
So once you have checked that, then you have to use this keyword next.
If you are not going to use this next keyword, it's not going to pass on through whatever next should
happen now.
What should happen next?
Maybe another middleware, maybe another dashboard access or maybe three or four in between checks and
then the access of dashboard.
It can happen.
Anything similar to the vendor operation?
Maybe you are going to one vendor only and collecting cache and depositing maybe are going through five
different vendors and then going to collect the cache or deposit the cache so this can happen.
So in the first phase, we have decided that we will check whether the user is logged in or not.
And in the second phase again since the next is there, that's only the reason why it is moving into
the next phase.
It says that, hey, I want to check whether you have an admin role or.
A manager role and then only allow you to actually get that.
So once this is all done, then only I'll allow you to access the dashboard.
Probably you can have one more to check.
Something else that you want to have a check.
Whether you have created an account this year or after this year, I want to redirect to this dashboard
or a modern dashboard.
So it's totally up to you how you decide it.
And this is how the basic minimum code looks like.
Previously we were having this request and response, but now we also inject the next as a keyword.
And once we have done with all the functionality, we actually go ahead and use the next.
Now the last three double dots actually are the representation that nothing happens after the next is
done.
This means almost like sending a response or taking, Hey, take the control back from me.
I don't know what should happen after that.
And that's the whole idea.
So this is the bare minimum basics of what middleware is.
Once you understand this middleware is there, then we can actually go ahead and take down the situation
of how we can take down the password.
In the next video, I will walk you through with the two possible scenario that can happen.
And then in the next one we are going to write the code to how to solve that problem.
Let's go ahead and move on to the next video.