1
00:00:00,220 --> 00:00:04,700
OK so the last thing that I want to do is refactor the middleware.

2
00:00:05,010 --> 00:00:13,380
So what that means is basically right now in a campground route we have check campground ownership and

3
00:00:13,380 --> 00:00:21,600
we have is logged in and we're duplicating is logged in over here where we have is logged in for comments

4
00:00:21,610 --> 00:00:21,780
.

5
00:00:21,990 --> 00:00:24,680
And then again check comment ownership.

6
00:00:24,780 --> 00:00:29,930
So we're just going to refactor all of this move those methods into a new file.

7
00:00:29,940 --> 00:00:37,710
So I'm actually going to make a directory in the top level of our app make directory middleware and

8
00:00:37,710 --> 00:00:42,120
then inside of there touch middleware.

9
00:00:42,210 --> 00:00:47,550
And I'm just going to combine all the middleware into one file so we could have campground middleware

10
00:00:47,580 --> 00:00:52,670
and then we could have comment middleware but can it make one file because we don't have that many.

11
00:00:53,040 --> 00:00:54,630
And I'm going to call it indexed.

12
00:00:54,690 --> 00:00:58,600
J.S. and I'll show you why rather than calling it middleware.

13
00:00:58,880 --> 00:01:03,060
Yes I'm calling it index inside the middleware directory.

14
00:01:03,060 --> 00:01:07,290
Now let's open that up.

15
00:01:09,460 --> 00:01:18,180
And then in here all the middleware goes here and then we also need to add in our module that exports

16
00:01:18,300 --> 00:01:19,750
equal to something.

17
00:01:19,980 --> 00:01:21,560
So we need some objects.

18
00:01:21,810 --> 00:01:25,050
And I've shown you a few different ways of doing this.

19
00:01:25,050 --> 00:01:31,420
We're going to call a variable called middleware object that doesn't exist yet to obey J.

20
00:01:31,710 --> 00:01:34,120
And that will contain all of the methods.

21
00:01:34,410 --> 00:01:41,340
So we'll define that up here and we'll just make it an empty object to start and then we'll add in some

22
00:01:41,340 --> 00:01:49,260
lines that look like this middleware object dot and then we'll have check campground ownership equals

23
00:01:49,340 --> 00:02:00,090
a function and then we'll have middleware objects dot check comment ownership equals a function as well

24
00:02:00,100 --> 00:02:01,070
and so on.

25
00:02:01,080 --> 00:02:06,750
So we're adding all of these functions onto middleware object and then we're setting modules that exports

26
00:02:06,750 --> 00:02:08,430
to be middleware object.

27
00:02:08,580 --> 00:02:15,390
The other way that I've shown this to you is just like this where instead of here we just add in check

28
00:02:16,050 --> 00:02:19,280
campground ownership as a function which is also fine.

29
00:02:19,290 --> 00:02:25,320
But I just want to show you another syntax rather than defining all of the functions at once when we

30
00:02:25,320 --> 00:02:26,240
define the object.

31
00:02:26,280 --> 00:02:30,600
We can do it after the fact and then the other way that you'll see it occasionally.

32
00:02:30,750 --> 00:02:35,960
It's just like this where there is no variable.

33
00:02:36,370 --> 00:02:38,160
You just have an object right here.

34
00:02:38,850 --> 00:02:41,380
And then all of your data goes in there.

35
00:02:42,210 --> 00:02:43,860
So all of those will work.

36
00:02:44,310 --> 00:02:52,710
We'll do middleware object to find that as an object and then we'll add in our code.

37
00:02:52,770 --> 00:03:04,770
So we start middleware object dot check campground ownership equals the giant function and we'll go

38
00:03:04,770 --> 00:03:12,570
ahead and copy that from campground Yes or campground just copy this whole thing over we'll cut it out

39
00:03:14,460 --> 00:03:20,490
and we will have to make a change or two because we don't need to define that name again but we do need

40
00:03:20,670 --> 00:03:26,970
those arguments that I care of one too many curly braces as well.

41
00:03:27,870 --> 00:03:31,350
That's outdone this little ever go.

42
00:03:31,380 --> 00:03:32,590
Looks good.

43
00:03:32,610 --> 00:03:35,640
We'll do the exact same thing this time.

44
00:03:35,640 --> 00:03:41,510
Check ups check comments.

45
00:03:43,710 --> 00:03:48,200
And let's go copy that right here.

46
00:03:48,790 --> 00:03:54,490
Cut it out as well.

47
00:03:55,300 --> 00:03:56,150
Pay set in

48
00:03:58,760 --> 00:04:06,470
and you want function request response and next out this as well.

49
00:04:06,810 --> 00:04:08,460
Either way.

50
00:04:08,460 --> 00:04:09,050
Great.

51
00:04:09,060 --> 00:04:12,560
And then we have our last one which is is logged in.

52
00:04:12,630 --> 00:04:20,710
So cut it out of here and I'll cut it out of here as well it's identical.

53
00:04:21,780 --> 00:04:22,590
OK.

54
00:04:22,590 --> 00:04:29,130
Now let's go to our middleware and just add that in to get at the bottom middle where object is logged

55
00:04:29,190 --> 00:04:35,180
in equals function request response next.

56
00:04:35,220 --> 00:04:35,930
Same code.

57
00:04:35,970 --> 00:04:42,300
We just moved it to this new file and we added everything into an object's middleware object that were

58
00:04:42,300 --> 00:04:45,560
then much of that exporting at the end.

59
00:04:45,660 --> 00:04:46,560
Great.

60
00:04:46,560 --> 00:04:53,370
Now we need to require this file index such as inside of the middleware directory.

61
00:04:53,370 --> 00:05:00,150
So let's start with campground and up top we'll just require middleware and to do that we need to first

62
00:05:00,150 --> 00:05:04,930
figure out based off of where we are which is instead of routes campground.

63
00:05:05,020 --> 00:05:08,840
Yes how do we get to index that address instead of middleware.

64
00:05:09,090 --> 00:05:16,170
Well we need to back out one directory up into the 10 and then we need to go in to middleware.

65
00:05:16,170 --> 00:05:24,230
So we need a VAR we'll call it middleware equals require and it's a dot dot slash.

66
00:05:24,300 --> 00:05:34,070
So that gets us into the 10 slash middleware and we could do this index.

67
00:05:34,200 --> 00:05:34,680
Yes.

68
00:05:34,830 --> 00:05:40,770
But the reason that I wanted to show you I want us to name it indexed us is that there is actually a

69
00:05:40,770 --> 00:05:49,230
special name that if we require a directory but not a file if I just require middleware it will automatically

70
00:05:49,230 --> 00:05:51,890
require the contents of index Dot.

71
00:05:51,960 --> 00:05:57,550
Yes that's supposed to be like the home where the main file where other things are required.

72
00:05:57,570 --> 00:06:03,830
So actually if we take a look in the node modules directory where are you inside.

73
00:06:03,930 --> 00:06:04,780
There we go.

74
00:06:05,130 --> 00:06:07,410
Let's take a look at Express.

75
00:06:07,530 --> 00:06:15,840
You can see there's an index such as file and index that Julius is the one file that is actually required

76
00:06:15,930 --> 00:06:21,130
when we require the Express directory and it requires a bunch of other files in turn.

77
00:06:21,130 --> 00:06:23,710
And those files might require a bunch of others.

78
00:06:24,390 --> 00:06:25,420
But that's really important.

79
00:06:25,440 --> 00:06:33,210
Index such as is a special name so we don't have to do slash index such as we just leave it as dot dot

80
00:06:33,210 --> 00:06:35,330
sized middleware.

81
00:06:35,430 --> 00:06:40,590
Now we need to use middleware because none of these functions are defined is logged in

82
00:06:43,290 --> 00:06:44,330
check campground ownership.

83
00:06:44,330 --> 00:06:45,440
Those don't exist.

84
00:06:45,600 --> 00:06:52,500
They're all inside of the middleware objects now just like that middleware dot is logged in mean the

85
00:06:52,500 --> 00:07:00,060
same thing for is logged in here and here and here.

86
00:07:00,330 --> 00:07:03,750
And finally there we'll save.

87
00:07:04,230 --> 00:07:09,530
And we need to do the same thing in our comments file.

88
00:07:09,570 --> 00:07:10,580
There we go.

89
00:07:11,220 --> 00:07:12,930
And we just want middleware.

90
00:07:13,020 --> 00:07:19,590
Dot is right in the middle where Dot is logged in.

91
00:07:19,590 --> 00:07:23,290
Same thing for check common ownership check common ownership.

92
00:07:23,370 --> 00:07:26,220
And one more check comment ownership.

93
00:07:26,220 --> 00:07:28,030
So what we've done is two things.

94
00:07:28,050 --> 00:07:32,070
One we've really cleaned up the route's files a little bit.

95
00:07:32,070 --> 00:07:35,540
They don't have those extra middleware at the bottom which are pretty long actually.

96
00:07:35,640 --> 00:07:36,560
If you take a look.

97
00:07:36,930 --> 00:07:43,590
But we also are no longer writing this code twice is logged in and any other future middleware we may

98
00:07:43,590 --> 00:07:46,110
need that we're going to share in different files.

99
00:07:46,110 --> 00:07:47,290
We don't have duplicate.

100
00:07:47,340 --> 00:07:49,310
We just add them to this file.

101
00:07:49,350 --> 00:07:49,670
All right.

102
00:07:49,680 --> 00:07:50,780
Let's make sure it works.

103
00:07:50,880 --> 00:07:53,220
As a final test.

104
00:07:53,220 --> 00:07:54,650
No problems here which is good

105
00:07:57,690 --> 00:08:00,760
and everything looks OK but we actually have a problem.

106
00:08:00,810 --> 00:08:08,010
If I clicked delete this illustrates it tells me campground is not defined inside of middleware slash

107
00:08:08,010 --> 00:08:13,350
index such as Line 6 middleware object at check campground ownership.

108
00:08:13,380 --> 00:08:21,840
If we take a look inside of our middleware file check campground ownership tells me campground wasn't

109
00:08:21,840 --> 00:08:24,500
defined a month 6.

110
00:08:25,290 --> 00:08:28,540
What I need to do is require campground.

111
00:08:28,950 --> 00:08:36,910
So far campground based off of where we are inside of the middleware directory technically of the 10

112
00:08:37,370 --> 00:08:48,090
I need to do require dot dot slash models slash campgrounds and then duplicate it and do the same thing

113
00:08:48,150 --> 00:08:49,470
for comments.

114
00:08:49,980 --> 00:08:56,130
Require that Slash model slash comments save once again restart

115
00:08:59,260 --> 00:09:04,670
go back refresh log in again.

116
00:09:05,100 --> 00:09:11,090
Sirius Black.

117
00:09:11,220 --> 00:09:14,810
Now if I try and delete this everything works fine.

118
00:09:14,850 --> 00:09:16,360
And dang it.

119
00:09:16,530 --> 00:09:18,360
Now I'm down another campground.

120
00:09:18,780 --> 00:09:21,100
Oh the things I do for this class.

121
00:09:21,490 --> 00:09:26,740
OK so just double check and it still works as well.

122
00:09:27,840 --> 00:09:28,810
There we go.

123
00:09:29,210 --> 00:09:34,830
So what was happening is that we didn't require the correct dependencies and we didn't know that until

124
00:09:34,830 --> 00:09:38,880
we actually tried to run the code once required campground to comment.

125
00:09:38,940 --> 00:09:40,500
Everything is good to go.

126
00:09:40,950 --> 00:09:46,550
All right so all we really did there was move some code out into a separate file and require that file

127
00:09:47,130 --> 00:09:52,950
but it cleaned up our routes a little bit and it's also nice because now we have other middleware that

128
00:09:52,950 --> 00:09:54,570
we need to add in.

129
00:09:54,570 --> 00:09:59,580
We have a little structure set up a place to put them and it's very easy to require them all over the

130
00:09:59,580 --> 00:10:00,840
place.

131
00:10:00,840 --> 00:10:01,310
Awesome.

132
00:10:01,500 --> 00:10:05,850
So that wraps up full crud for a campground in comments.

133
00:10:05,910 --> 00:10:12,240
You can now create read update and delete campgrounds and you can create read update and delete comments

134
00:10:12,240 --> 00:10:18,240
as well and those permissions in there so there's authorization and authentication so you can't create

135
00:10:18,240 --> 00:10:20,040
something you for not logged in.

136
00:10:20,250 --> 00:10:25,590
You can't edit something or delete something if you're not logged in and if it doesn't belong to you

137
00:10:26,010 --> 00:10:31,830
but you can read a comment or you can read a campground if you're logged in or not it doesn't matter

138
00:10:31,830 --> 00:10:32,030
.

139
00:10:32,040 --> 00:10:33,840
Same thing for comments.

140
00:10:33,840 --> 00:10:36,630
So we have three kind of levels of permissions.

141
00:10:36,630 --> 00:10:37,840
One is you can do any.

142
00:10:37,860 --> 00:10:44,040
You can read something for not logged in to is you can create something if you are logged in doesn't

143
00:10:44,040 --> 00:10:45,310
matter who you are though.

144
00:10:45,360 --> 00:10:49,890
In theory you can edit and update something but it does depend who you are.

145
00:10:49,890 --> 00:10:55,200
Next up we're going to add in some other fun features and the one that hopefully is the most exciting

146
00:10:55,200 --> 00:11:00,600
or that will at least make the app more usable is something called Flash messages so it's a way to see

147
00:11:00,600 --> 00:11:06,300
an error message that pops up that says successfully created this or you don't have permission to do

148
00:11:06,300 --> 00:11:10,930
that or Please sign in or password incorrect so it makes things a lot more usable.

149
00:11:11,190 --> 00:11:12,180
So we'll see that soon.
