1
00:00:04,680 --> 00:00:06,000
Welcome back.

2
00:00:06,030 --> 00:00:13,650
In this video, let's go ahead and create a military to protect a route, meaning that unless you are

3
00:00:13,650 --> 00:00:17,880
looking before you can access a certain endpoint.

4
00:00:18,000 --> 00:00:21,570
So let me comment this one from the previous video.

5
00:00:21,570 --> 00:00:28,350
So now let me come up here and let's start with our custom media world.

6
00:00:28,770 --> 00:00:34,120
In this video, I will also show you a different way of using middleware.

7
00:00:34,140 --> 00:00:35,600
So here we go.

8
00:00:35,640 --> 00:00:39,420
Let me give it a beautiful common sense protected.

9
00:00:42,070 --> 00:00:44,740
Drought middle.

10
00:00:45,620 --> 00:00:46,460
Where?

11
00:00:47,170 --> 00:00:48,430
So here we go.

12
00:00:48,550 --> 00:00:55,840
Since we have not talked about authentication or authorization, we are just trying to use a dummy authenticated

13
00:00:55,840 --> 00:00:56,930
user details.

14
00:00:56,950 --> 00:00:58,000
So here we go.

15
00:00:58,420 --> 00:01:07,660
Make use of counts and call this one as protected and going to be my arrow function and ise dot and

16
00:01:07,660 --> 00:01:12,730
I have access to request and then response and then the next function.

17
00:01:12,970 --> 00:01:20,110
So inside a function body, let's provide this user details assuming that we got this user logging details

18
00:01:20,110 --> 00:01:21,370
from a database.

19
00:01:21,370 --> 00:01:25,870
So let me call this one user log in details.

20
00:01:26,770 --> 00:01:34,210
So the first property here is going to be is locked in and then let me give it as true for the first

21
00:01:34,210 --> 00:01:41,050
time and let's provide the user name and let me call this one as John.

22
00:01:41,050 --> 00:01:49,480
So first is we are waiting to check if a user is logged in, then we can go ahead and proceed on to

23
00:01:49,480 --> 00:01:50,440
the next material.

24
00:01:50,470 --> 00:01:53,080
Otherwise we are going to throw an error.

25
00:01:53,110 --> 00:01:54,310
So here we go.

26
00:01:54,370 --> 00:02:04,690
First is let's check if user login details DOT is logged in, which is equal to true, then we will

27
00:02:04,690 --> 00:02:08,320
go ahead and call the next major word.

28
00:02:09,229 --> 00:02:13,160
Otherwise errors, we are going to throw an arrow.

29
00:02:13,190 --> 00:02:17,240
So here we can make use of return when return.

30
00:02:17,270 --> 00:02:21,800
It means that we are exiting from this function and the other function down.

31
00:02:21,800 --> 00:02:23,070
It will never run.

32
00:02:23,090 --> 00:02:25,550
So let's return from this major word.

33
00:02:25,700 --> 00:02:26,870
So here we go.

34
00:02:26,900 --> 00:02:30,320
Make use of red dot, Jason.

35
00:02:30,470 --> 00:02:36,650
So inside the Jason let's provide a simple text says you must.

36
00:02:39,050 --> 00:02:41,150
Looking first.

37
00:02:41,150 --> 00:02:45,750
So now we have our major way to put it a route again.

38
00:02:45,770 --> 00:02:50,450
This is our user login details and by default login is true.

39
00:02:50,600 --> 00:02:58,010
So for this function, if this one is true, it means that we are calling next and then after calling

40
00:02:58,010 --> 00:03:02,420
next, any of this route that we use, we are able to see the response.

41
00:03:02,420 --> 00:03:03,890
So how can we use it?

42
00:03:04,040 --> 00:03:06,590
Well, we can use it in two ways.

43
00:03:06,590 --> 00:03:10,100
One is by passing it into apt use.

44
00:03:10,100 --> 00:03:11,510
So here we go.

45
00:03:11,540 --> 00:03:12,830
App dot.

46
00:03:13,640 --> 00:03:21,860
App dot use as we did before then I'll provide protected so when provide to update use is going to be

47
00:03:21,860 --> 00:03:29,780
apply to any of the route, whether it's get or post or home post or create post is going to be called.

48
00:03:29,780 --> 00:03:30,890
So now check it out.

49
00:03:31,100 --> 00:03:39,290
Now when I make a request and can see that I get a message that posts fetch meaning that the user is

50
00:03:39,290 --> 00:03:40,190
logged in.

51
00:03:40,460 --> 00:03:47,240
But if I change this one to force, it means that this code block would never be called.

52
00:03:47,450 --> 00:03:49,160
Then it will get here.

53
00:03:49,160 --> 00:03:50,480
Then I get this message.

54
00:03:50,480 --> 00:03:51,650
Now check it out.

55
00:03:51,890 --> 00:03:56,000
If I make the request to see that, you must log in first.

56
00:03:56,000 --> 00:03:57,890
And I didn't see my resources.

57
00:03:57,890 --> 00:03:59,720
So it means that it's working.

58
00:03:59,960 --> 00:04:04,160
And if I hit any of these endpoints, let's see.

59
00:04:06,170 --> 00:04:08,090
Create post or even homepage.

60
00:04:08,090 --> 00:04:09,960
So let's go to homepage.

61
00:04:09,980 --> 00:04:18,050
When I built the first slash, when I hit st, you see, I still see you must look in and if I go to

62
00:04:18,050 --> 00:04:24,320
create post, which is post and post large post here and hit st you see.

63
00:04:24,320 --> 00:04:25,460
I still see.

64
00:04:25,460 --> 00:04:27,050
You must log in first.

65
00:04:27,970 --> 00:04:33,390
Even though there are some rout, which I say that is public, for example, fetching or post here.

66
00:04:33,400 --> 00:04:39,850
But if I try to fetch or post also is going to tell me that you must login first.

67
00:04:39,850 --> 00:04:44,680
So using app to use it applies to all routes.

68
00:04:44,710 --> 00:04:52,000
But if you want to apply a certain material to a specific route, this is how we are going to use it.

69
00:04:52,150 --> 00:04:59,440
We are going to pass in to the app to use, but instead we pass into a specific route.

70
00:04:59,440 --> 00:05:04,270
So now for this route, let's see which of this one do I want to protect?

71
00:05:04,270 --> 00:05:06,520
And that is the authenticated route.

72
00:05:06,550 --> 00:05:07,690
It depends on you.

73
00:05:07,720 --> 00:05:09,340
So this is how you make use of it.

74
00:05:09,520 --> 00:05:16,510
After the path and before the request handler, you provide a midway here and here we go is going to

75
00:05:16,510 --> 00:05:23,260
be as protected and common, so let's save it now.

76
00:05:23,260 --> 00:05:27,490
It means that the metaphor applies to this endpoint for creating posts.

77
00:05:27,490 --> 00:05:36,460
So if I go to fetch or post here, I make use of post which is Pura and let me change the HTTP method

78
00:05:36,460 --> 00:05:37,240
to get.

79
00:05:37,240 --> 00:05:41,260
And now when I hit send it can see that I get fetch posts.

80
00:05:41,440 --> 00:05:47,830
But if I change this one to create when I hit send, it could see that I don't get that endpoint.

81
00:05:47,830 --> 00:05:50,170
The endpoint says create post.

82
00:05:50,680 --> 00:05:56,260
Sorry on my side when I hit send it can see that it has been called.

83
00:05:56,260 --> 00:05:58,090
You must log in first.

84
00:05:58,120 --> 00:06:05,590
This is how we can protect a route and create a middleware to pass into a specific endpoint.

85
00:06:05,680 --> 00:06:09,910
So the next video, let's go ahead and create a middleware.

86
00:06:09,910 --> 00:06:15,130
If a user is an admin, then you can go ahead and delete a post.

87
00:06:15,520 --> 00:06:16,030
All right.

88
00:06:16,030 --> 00:06:18,130
Let's get into that in the next video.

