1
00:00:08,130 --> 00:00:09,480
Welcome back.

2
00:00:09,510 --> 00:00:18,150
In this video, let's see how we can protect a route by implementing what is called middleware.

3
00:00:18,300 --> 00:00:22,320
So back to the code and inside the server.

4
00:00:22,560 --> 00:00:31,380
Before we get into that, let's try to refactor this code over here because we may need this code that

5
00:00:31,380 --> 00:00:37,620
is getting the token from the header in any route that we want to have access to the user.

6
00:00:37,650 --> 00:00:42,720
So let's go ahead and create one for the core middleware.

7
00:00:42,720 --> 00:00:51,690
And it's going to have our middleware, for example, is logging or is an admin and many more.

8
00:00:51,990 --> 00:00:56,840
So for the meantime, let's work with a log in.

9
00:00:56,850 --> 00:01:03,640
So here is going to be a log in dot JS.

10
00:01:03,900 --> 00:01:04,349
Good.

11
00:01:04,349 --> 00:01:11,520
And remember because it's a memory you need to take in three agreement that is the.

12
00:01:12,700 --> 00:01:14,620
Requests and then response.

13
00:01:14,650 --> 00:01:17,040
So let's go ahead and do that.

14
00:01:17,050 --> 00:01:31,330
Cost is locked in equal to my function req response and next as that let's look at the roadmap here.

15
00:01:31,330 --> 00:01:40,810
Step number one is get the token from header.

16
00:01:41,170 --> 00:01:46,930
Then step number two is we want to verify the token.

17
00:01:48,980 --> 00:01:52,580
And if everything goes on, well, we need to save.

18
00:01:52,610 --> 00:01:54,410
Step number three is.

19
00:01:55,490 --> 00:02:06,170
Save the user into rec into rec object before we move on.

20
00:02:06,170 --> 00:02:14,840
This kind of authentication using token is mostly used in stock application because for this one we

21
00:02:14,840 --> 00:02:19,100
are dealing with words e.g. template that is HTML.

22
00:02:19,130 --> 00:02:25,940
And if we want to have a complete application using this approach, we need to write a lot of code.

23
00:02:25,940 --> 00:02:28,790
So I will show you what I mean as you move on.

24
00:02:28,790 --> 00:02:30,740
So let's finish up this one.

25
00:02:30,740 --> 00:02:34,130
So how can we get the token from a header?

26
00:02:34,130 --> 00:02:37,070
What is inside this root?

27
00:02:37,070 --> 00:02:44,600
So it means that we can extract this one and then put it inside a different folder called Utils.

28
00:02:44,600 --> 00:02:48,740
So let's create one folder called utility function folder.

29
00:02:48,890 --> 00:02:53,360
And the first function that we are going to create is called Get.

30
00:02:54,510 --> 00:02:58,340
Tolkien from header.

31
00:02:58,350 --> 00:03:00,900
The name is too long, but don't worry.

32
00:03:01,140 --> 00:03:04,740
So let's see how you're going to create that functionality.

33
00:03:04,920 --> 00:03:13,050
Well, it means that this function is going to take in the request object because this is what we are

34
00:03:13,050 --> 00:03:14,400
accessing here.

35
00:03:14,430 --> 00:03:22,530
Well, the responsibility of this function is to receive the entire request object, and then it is

36
00:03:22,530 --> 00:03:25,740
going to take the token from the header.

37
00:03:25,920 --> 00:03:33,510
So we are going to cut the logic inside this profile because this code takes in the token.

38
00:03:33,660 --> 00:03:39,150
So our cut everything from here and next is our paste in here.

39
00:03:39,180 --> 00:03:42,960
So before I do that, let me go back here.

40
00:03:42,990 --> 00:03:48,380
Before I do that, let's try to console.log the request object.

41
00:03:48,390 --> 00:03:53,740
In that way, you better understand the method that we are going to grab on that.

42
00:03:53,760 --> 00:04:02,370
So let's go ahead and then require the method called get to kin from header and I have the auto import

43
00:04:02,610 --> 00:04:03,180
start.

44
00:04:03,420 --> 00:04:11,190
So next is inside any route where I want to have access to the request object, I'm going to pass in

45
00:04:11,190 --> 00:04:12,450
that function.

46
00:04:12,600 --> 00:04:16,170
So let's truncate the profile route.

47
00:04:18,029 --> 00:04:19,490
Which is this one.

48
00:04:19,500 --> 00:04:24,570
So let's go ahead and assign to a variable call.

49
00:04:24,660 --> 00:04:33,600
My, my token is equal to get token from a header, then you pass in the request object.

50
00:04:33,960 --> 00:04:38,520
So now I can remove that because I'm console.log init from this function.

51
00:04:38,520 --> 00:04:46,230
So now let's go ahead and make the requests and now let's check our terminal and you can see that we

52
00:04:46,230 --> 00:04:50,820
have the header and this is what we want.

53
00:04:50,940 --> 00:04:57,060
You can see we have the properties and the authorization key is what we want.

54
00:04:57,060 --> 00:05:03,780
Now we have accessed the request object inside this get token from header.

55
00:05:03,780 --> 00:05:13,020
So now it's time for us to cut everything from here as that and let me remove this function for now.

56
00:05:13,020 --> 00:05:13,860
Let me maintain it.

57
00:05:13,860 --> 00:05:17,190
And now back here, last place it here.

58
00:05:17,490 --> 00:05:29,550
So now let's check if there is no token or if token is not equal to undefined, it means that there

59
00:05:29,550 --> 00:05:30,360
is token.

60
00:05:30,480 --> 00:05:32,850
So what are we supposed to do?

61
00:05:33,150 --> 00:05:35,580
If there is token, why not?

62
00:05:35,580 --> 00:05:39,000
We go ahead and then return the token.

63
00:05:39,480 --> 00:05:43,080
Otherwise we are going to return.

64
00:05:44,020 --> 00:05:53,380
And objects with some messages on that by providing status, let's say, failed.

65
00:05:53,620 --> 00:05:59,110
And the message we're going to be as.

66
00:06:06,650 --> 00:06:07,280
Good.

67
00:06:07,310 --> 00:06:09,080
So now let's see.

68
00:06:09,290 --> 00:06:11,950
Let's go ahead and use the function.

69
00:06:11,960 --> 00:06:13,280
Now we are using it here.

70
00:06:13,280 --> 00:06:16,160
So let me call this one and this function.

71
00:06:16,250 --> 00:06:18,160
Now, moment of truth.

72
00:06:18,170 --> 00:06:23,450
So if I go to the profile and make the request, well, I get something.

73
00:06:23,450 --> 00:06:23,870
Where?

74
00:06:23,870 --> 00:06:27,500
Here, meaning talking is not defined.

75
00:06:27,650 --> 00:06:35,330
So it means that we are not assigning the result coming back from this function as token.

76
00:06:35,510 --> 00:06:37,400
So it's equal to that.

77
00:06:38,090 --> 00:06:41,150
So let's go ahead and make the request again.

78
00:06:41,150 --> 00:06:44,540
And now you see that everything remains the same.

79
00:06:44,870 --> 00:06:54,980
But if I turn off the authorization or even the value here from this, let's see, let's see the response

80
00:06:54,980 --> 00:07:01,970
we're going to get now, meaning there is no token attached to that because at this point we pass in

81
00:07:01,970 --> 00:07:03,380
to the verification.

82
00:07:03,380 --> 00:07:05,600
So now it is working.

83
00:07:06,050 --> 00:07:09,740
So let's also move this function to it on file.

84
00:07:09,740 --> 00:07:12,650
So which is this function called verification?

85
00:07:13,100 --> 00:07:16,670
So let's go ahead and create a file to put this code on it.

86
00:07:16,670 --> 00:07:23,240
So let me cut it from here and let me go to the utils here and here we go.

87
00:07:23,240 --> 00:07:31,130
Going to be verified token, see how we are breaking down the concept and now let me paste it here.

88
00:07:31,220 --> 00:07:39,260
So let me export this function as module dot exports equal to verify token.

89
00:07:39,530 --> 00:07:47,600
So now inside the server file, let's go ahead and require the function called verify token.

90
00:07:47,600 --> 00:07:48,920
So here you go.

91
00:07:48,950 --> 00:07:53,720
So in any route that I want to verify, I'm going to use that function.

92
00:07:53,900 --> 00:07:57,560
So I want to verify here as that.

93
00:07:57,560 --> 00:08:01,760
Now we have it as before, so let's go ahead and make the request.

94
00:08:01,760 --> 00:08:03,770
Well, I get something wrong.

95
00:08:03,770 --> 00:08:04,880
Let's see.

96
00:08:05,390 --> 00:08:08,690
JWT is not defined simply.

97
00:08:08,690 --> 00:08:10,250
Is that on this file?

98
00:08:10,250 --> 00:08:15,110
We are making use of this module but is nowhere to be found.

99
00:08:15,110 --> 00:08:24,980
So let's go ahead and then require that package as JWT equal to required and then the function as JSON

100
00:08:24,980 --> 00:08:26,180
web token.

101
00:08:26,420 --> 00:08:37,190
Now let's see if everything is correct and we got NARA simply because in the header we are not passing

102
00:08:37,190 --> 00:08:38,909
in the bearer.

103
00:08:38,929 --> 00:08:45,470
So here we can also check some condition that if there is no header here, we can display error.

104
00:08:45,470 --> 00:08:50,390
For the meantime, let's bring back the token and let's see we got now.

105
00:08:50,390 --> 00:08:53,690
So let's log in again and get back.

106
00:08:53,690 --> 00:08:59,420
Sorry, this log in here is that log in root.

107
00:08:59,420 --> 00:09:00,890
So let's hit send.

108
00:09:00,890 --> 00:09:06,650
I have the token and let me and the profile.

109
00:09:07,130 --> 00:09:11,480
Let me change this one to the entire token here.

110
00:09:11,690 --> 00:09:18,200
And you can see that we go back the user and everything remains the same.

111
00:09:18,200 --> 00:09:20,870
So this is how we are able to refactor it.

112
00:09:20,960 --> 00:09:27,950
Well, the main concept that you want to do is we want to create a root for checking a log in.

113
00:09:28,130 --> 00:09:35,150
So let's continue the next video to finish up this logic, because right now we are almost through because

114
00:09:35,150 --> 00:09:41,810
we have these functions being on different file so we can go ahead and use it inside this module in

115
00:09:41,810 --> 00:09:42,920
the next video.

