WEBVTT

00:05.850 --> 00:07.380
Hey, did everyone share?

00:07.380 --> 00:09.710
And welcome to another video of this section.

00:09.720 --> 00:11.250
I hope you are enjoying this course.

00:11.250 --> 00:13.350
If you are enjoying it even a tiny bit, let me know.

00:13.350 --> 00:16.050
And then Instagram share a story, click a photo or something.

00:16.050 --> 00:21.060
I would be really, really happy to see your feedback and it motivates me to create more awesome courses.

00:21.060 --> 00:23.910
So we have applied a lot from this strategy.

00:23.910 --> 00:27.570
We have seen that how we can register and check already registered users.

00:27.570 --> 00:30.780
Now the problem in front of us is to take care of the password.

00:30.900 --> 00:34.980
I can directly introduce you what the solution is, but I'm not going to do it like that.

00:34.980 --> 00:39.450
I will introduce you to all the possible use case scenario that can happen so that whenever you are

00:39.450 --> 00:41.340
designing any back end, you know what?

00:41.340 --> 00:45.870
All situation goes in and you find out that yes, all of them situations are absolutely correct.

00:45.870 --> 00:48.630
You can choose any path, so it is enough.

00:48.750 --> 00:52.800
Now we cannot avoid one thing which is getting introduced to the middleware.

00:52.980 --> 00:58.500
A lot of people believe that middleware is a concept of express JS or some routing concept, which is

00:58.500 --> 00:59.280
not true.

00:59.280 --> 01:04.890
Yes, it is being used quite a lot in the routing and express, but it is equally used in mongoose,

01:04.890 --> 01:08.070
other libraries, other frameworks and in other languages as well.

01:08.070 --> 01:09.720
Like Python and Java.

01:09.720 --> 01:12.270
This concept is whole in general.

01:12.270 --> 01:17.580
So that's why a lot of people actually adopted a lot of frameworks and libraries adapted by no mean.

01:17.580 --> 01:21.990
This doesn't mean that this is only a concept to JavaScript or only to Node.js.

01:21.990 --> 01:23.100
It is not like that.

01:23.220 --> 01:25.590
So first, let's understand what is the middleware?

01:25.620 --> 01:28.590
Now, middleware is something which happens in between in the middle.

01:28.620 --> 01:33.720
Yes, I know this is super easy, but let's elaborate it a little bit so that you understand it permanently

01:33.720 --> 01:35.310
for the fullest of all.

01:35.400 --> 01:38.250
Now in the middleware, let's go with a very simple example.

01:38.250 --> 01:42.540
In fact, a couple of them, let's just say you are in the office and you want to go to home.

01:42.540 --> 01:46.140
But this is a weekend and you don't want to directly go to home.

01:46.140 --> 01:48.060
You are going to go first to the bakery.

01:48.090 --> 01:53.790
You decided that I'll buy myself a pizza, probably a soft drink, and then I'll go to the home.

01:53.790 --> 01:56.670
So what you did there, you did something in between.

01:56.670 --> 02:00.120
And that's the whole idea of having a concept of middleware.

02:00.120 --> 02:05.790
You do something in between now you actually pass on after that or not, that's up to you.

02:05.790 --> 02:09.990
And the second example will give you more idea of what middleware possibly can do.

02:10.080 --> 02:14.010
You are in the office and this is a working hour probably Monday morning.

02:14.010 --> 02:17.820
And this is you are working into a marketing job or a field job.

02:17.820 --> 02:20.550
And in that you have to go through with couple of vendors.

02:20.550 --> 02:26.190
Now, these vendors are somebody who takes some material or raw material from your organization, and

02:26.190 --> 02:28.530
this is your day where you want to collect some money.

02:28.530 --> 02:33.570
So you go to the vendor and this vendor says, Hey, you take a check from us and you go to the bank

02:33.570 --> 02:34.830
to deposit that check.

02:34.830 --> 02:39.000
But there is also a possibility saying that this vendor doesn't give you a check, but rather gives

02:39.000 --> 02:39.810
you a cash.

02:39.810 --> 02:45.000
So in that case, you simply go ahead, go to your collection office and deposit that check so you can

02:45.000 --> 02:50.150
see in this case, the middle weight is heavily influencing you what you really should do next up.

02:50.160 --> 02:53.640
And if the vendor is not giving you anything, you should not proceed further.

02:53.640 --> 02:56.340
This is one way example, but this can also happen.

02:56.460 --> 03:01.350
So again, moving on to this same scenario, what it can happen, let's go ahead and take it to the

03:01.350 --> 03:02.640
technical side of it.

03:02.670 --> 03:07.710
Now, this is some kind of check or functionality that is injected in between what?

03:07.710 --> 03:08.400
In between?

03:08.400 --> 03:13.530
Yes, it can happen in the root as just we saw, but it can happen in the process of doing something

03:13.530 --> 03:18.270
like, for example, you want to save something into the database just before the save happen, do something

03:18.270 --> 03:20.550
or just after the save happen, do something.

03:20.550 --> 03:21.870
They are also middleware.

03:21.870 --> 03:24.930
So it doesn't mean that the middleware should happen only in between.

03:24.930 --> 03:27.150
It can happen just after that as well.

03:27.360 --> 03:31.680
Sometimes this can also be called as a life cycle event or life cycle hooks as well.

03:31.680 --> 03:36.330
Totally fine as a definition as long as you get the idea and what the concept in between.

03:36.330 --> 03:40.320
We are not in the phase that we should be fighting about what we are calling it.

03:40.320 --> 03:42.960
That's not what we are going to do doing up here.

03:43.110 --> 03:47.700
Now, since we are talking more about the Node.js friendly environment and especially Express and everything,

03:47.700 --> 03:50.280
you should also know about the next keyword.

03:50.280 --> 03:55.770
Now this keyword is really important if you're going to be writing any kind of middleware at all, let's

03:55.770 --> 04:01.110
go ahead and see what this keyword is responsible and how it is going to inject its functionality in

04:01.110 --> 04:03.420
the perspective of the web development.

04:03.810 --> 04:09.300
So let's just say a request comes in to you or your application and you decide that, hey, I am not

04:09.300 --> 04:13.770
going to allow this request is, by the way, to access the dashboard, probably an admin dashboard.

04:13.770 --> 04:16.470
So you have designed multiple functionality in between.

04:16.470 --> 04:20.970
You have said that, hey, this is a functionality or feature to check it whether you are logged in

04:20.970 --> 04:21.540
or not.

04:21.540 --> 04:25.020
So once you have checked that, then you have to use this keyword next.

04:25.020 --> 04:28.950
If you are not going to use this next keyword, it's not going to pass on through whatever next should

04:28.950 --> 04:29.940
happen now.

04:29.940 --> 04:30.900
What should happen next?

04:30.900 --> 04:37.530
Maybe another middleware, maybe another dashboard access or maybe three or four in between checks and

04:37.530 --> 04:38.670
then the access of dashboard.

04:38.670 --> 04:39.180
It can happen.

04:39.180 --> 04:41.280
Anything similar to the vendor operation?

04:41.280 --> 04:46.140
Maybe you are going to one vendor only and collecting cache and depositing maybe are going through five

04:46.140 --> 04:51.180
different vendors and then going to collect the cache or deposit the cache so this can happen.

04:51.180 --> 04:55.140
So in the first phase, we have decided that we will check whether the user is logged in or not.

04:55.140 --> 05:00.210
And in the second phase again since the next is there, that's only the reason why it is moving into

05:00.210 --> 05:01.110
the next phase.

05:01.110 --> 05:04.170
It says that, hey, I want to check whether you have an admin role or.

05:04.280 --> 05:07.580
A manager role and then only allow you to actually get that.

05:07.580 --> 05:11.450
So once this is all done, then only I'll allow you to access the dashboard.

05:11.450 --> 05:13.220
Probably you can have one more to check.

05:13.220 --> 05:15.590
Something else that you want to have a check.

05:15.590 --> 05:21.050
Whether you have created an account this year or after this year, I want to redirect to this dashboard

05:21.050 --> 05:22.070
or a modern dashboard.

05:22.070 --> 05:24.350
So it's totally up to you how you decide it.

05:24.350 --> 05:26.990
And this is how the basic minimum code looks like.

05:26.990 --> 05:31.760
Previously we were having this request and response, but now we also inject the next as a keyword.

05:31.760 --> 05:36.230
And once we have done with all the functionality, we actually go ahead and use the next.

05:36.230 --> 05:41.870
Now the last three double dots actually are the representation that nothing happens after the next is

05:41.870 --> 05:42.260
done.

05:42.260 --> 05:46.910
This means almost like sending a response or taking, Hey, take the control back from me.

05:46.910 --> 05:48.590
I don't know what should happen after that.

05:48.590 --> 05:50.090
And that's the whole idea.

05:50.090 --> 05:53.390
So this is the bare minimum basics of what middleware is.

05:53.390 --> 05:58.610
Once you understand this middleware is there, then we can actually go ahead and take down the situation

05:58.610 --> 06:00.530
of how we can take down the password.

06:00.530 --> 06:04.940
In the next video, I will walk you through with the two possible scenario that can happen.

06:04.940 --> 06:09.260
And then in the next one we are going to write the code to how to solve that problem.

06:09.260 --> 06:11.180
Let's go ahead and move on to the next video.
