1
00:00:00,330 --> 00:00:03,270
Nice work, we're actually done with the main functionality.

2
00:00:04,080 --> 00:00:06,780
Just like in a previous project, once everything is in place.

3
00:00:07,110 --> 00:00:11,970
Let's also work on some improvements which will use heavily in the following project.

4
00:00:12,330 --> 00:00:15,420
And first, I want to set up the authentication middleware.

5
00:00:15,930 --> 00:00:18,450
And essentially the idea is following where?

6
00:00:18,450 --> 00:00:25,950
Yes, at the moment we have the dashboard and there were successfully checking whether the JSON Web

7
00:00:25,950 --> 00:00:27,720
token has been provided.

8
00:00:27,900 --> 00:00:31,170
And of course, if it's there, then we send back the lucky number.

9
00:00:31,170 --> 00:00:37,670
If not, then we throw the error where we need to understand that in a more realistic application,

10
00:00:37,680 --> 00:00:42,090
of course, there's going to be multiple routes that use this functionality.

11
00:00:42,420 --> 00:00:43,590
And what are we going to do?

12
00:00:43,880 --> 00:00:47,250
Are we really going to copy and paste for every route that needs it?

13
00:00:47,550 --> 00:00:54,720
Or it just makes more sense to take all of this code, stick it in the middleware and then just choose

14
00:00:54,720 --> 00:00:57,210
which routes need to be authenticated.

15
00:00:57,490 --> 00:01:00,720
And of course, the answer is setting up our own middleware.

16
00:01:01,110 --> 00:01:04,670
So first, let's go to the middleware directly in there.

17
00:01:04,680 --> 00:01:09,660
You'll find off Jass and we'll just start very simply by creating a function.

18
00:01:09,960 --> 00:01:14,310
That function, of course, is going to be a sync and we'll be looking for work.

19
00:01:14,910 --> 00:01:21,900
And next and next is very, very important because otherwise our whole cycle is going to break.

20
00:01:22,200 --> 00:01:27,180
And then, of course, we just want to console log in the beginning will just come along and then we'll

21
00:01:27,180 --> 00:01:28,230
set up the functionality.

22
00:01:28,650 --> 00:01:31,530
Let's go over here is always naming is really up to you.

23
00:01:31,860 --> 00:01:35,280
We're going to go with authentication and then middleware.

24
00:01:35,280 --> 00:01:37,860
And that is equal to, like I said, async.

25
00:01:38,220 --> 00:01:41,040
Then we go with a request and response.

26
00:01:41,040 --> 00:01:45,510
And then next, because remember, in order to move on to the next middleware, which of course in our

27
00:01:45,510 --> 00:01:51,330
case is going to be a dashboard, we'll have to call MEXT and I'm going to go with console log that

28
00:01:51,330 --> 00:01:57,900
I'm looking for Iraq at first and more specifically looking for the authorization one.

29
00:01:58,280 --> 00:02:04,110
I just want to check whether it exists and I want to go next, like I said.

30
00:02:04,380 --> 00:02:06,780
And then, of course, we invoke it now.

31
00:02:06,780 --> 00:02:08,460
We just need to explore this.

32
00:02:08,460 --> 00:02:12,000
So we're going to go over your module and exports.

33
00:02:13,040 --> 00:02:20,420
And I will be equal to my authentication middleware function and now another question.

34
00:02:20,450 --> 00:02:24,110
So where are we going to use this middleware now in a later?

35
00:02:24,110 --> 00:02:30,600
Projects will actually use it on multiple routes, but in this case, we only have a dashboard button,

36
00:02:30,630 --> 00:02:31,550
is that correct?

37
00:02:31,850 --> 00:02:37,750
So as far as the login, this is going to be for public access when it comes to that.

38
00:02:37,790 --> 00:02:41,690
But this is going to be protected by that authentication law.

39
00:02:41,990 --> 00:02:48,110
So therefore, in the router in the manageress, we're going to go here, CONSED and then of course

40
00:02:48,110 --> 00:02:49,970
I'm looking for my function.

41
00:02:50,210 --> 00:02:55,280
And in this case I'm just going to call this off where I'm not as equal to or require.

42
00:02:55,280 --> 00:03:02,330
Of course, then we had the middleware folder and the auth file and the syntax is following where where

43
00:03:02,330 --> 00:03:03,650
we have this great method.

44
00:03:04,610 --> 00:03:12,050
Before the dashboard actually want to stick my magloire, so I'm going to say off and then Minoa and

45
00:03:12,050 --> 00:03:12,970
I'll add comma.

46
00:03:13,310 --> 00:03:19,550
So now, of course, every time someone is going to be hitting this route first, they will go through

47
00:03:19,550 --> 00:03:22,550
the middle way and of course, in the middle worst since I have next.

48
00:03:22,880 --> 00:03:28,370
Then I'll pass to the dashboard and not to start out, I'm just going to navigate to the browser.

49
00:03:28,610 --> 00:03:34,430
So this is where I have the working application and it doesn't really matter whether you're logged in

50
00:03:34,430 --> 00:03:34,880
or not.

51
00:03:35,180 --> 00:03:36,970
Just go here to get data.

52
00:03:37,400 --> 00:03:39,770
And of course, in my case, I haven't logged in.

53
00:03:39,770 --> 00:03:40,700
I have not taken.

54
00:03:40,910 --> 00:03:42,410
So therefore I'm getting back.

55
00:03:42,410 --> 00:03:49,760
That's not authorized access this route or what I'm more interested is back in my application in the

56
00:03:49,760 --> 00:03:51,320
car so I can see better.

57
00:03:51,530 --> 00:03:52,910
And that is Saturno.

58
00:03:53,180 --> 00:03:59,860
So now I know that all the requests that are going to the dashboard are actually hitting this off the

59
00:03:59,870 --> 00:04:00,710
middle ground first.

60
00:04:01,070 --> 00:04:04,810
And of course, we just need to add the functionality and we'll be in good shape.

