1
00:00:00,360 --> 00:00:05,250
We'll come back in this video we're basically going to do the same thing we did in the last video with

2
00:00:05,250 --> 00:00:09,510
users and comments but we're going to do it for users and campgrounds.

3
00:00:09,510 --> 00:00:10,830
There are two main ideas.

4
00:00:10,830 --> 00:00:15,640
The first is that we don't want someone submitting a new campground if they're not signed in.

5
00:00:15,690 --> 00:00:18,190
We don't want anyone just to go and create a campground.

6
00:00:18,200 --> 00:00:19,280
Must they have an account.

7
00:00:19,500 --> 00:00:21,290
Anyone can view the campground.

8
00:00:21,360 --> 00:00:26,100
Anyone can read comments but you can't submit a new campground or a new comment unless you're signed

9
00:00:26,100 --> 00:00:26,520
in.

10
00:00:26,820 --> 00:00:31,680
And the second one is not only do we want to make sure you're signed in but we also want to save who

11
00:00:31,680 --> 00:00:37,440
you are your username and ID to the newly created campground just like we did for the comments so that

12
00:00:37,470 --> 00:00:44,490
on a campground show page we can show who created it created by potato created by resti and then that

13
00:00:44,490 --> 00:00:50,310
also has implications later on for who can delete particular comments or campgrounds and we need the

14
00:00:50,310 --> 00:00:52,360
ID and the user name for that to work.

15
00:00:52,440 --> 00:00:57,090
We'll start with the easy part which is preventing someone who's not logged in from creating a campground

16
00:00:57,100 --> 00:00:57,330
.

17
00:00:57,750 --> 00:01:01,880
So we need to open up the campground routes which are an set of routes campgrounds.

18
00:01:01,970 --> 00:01:09,010
J.S. and the important routes here are the Create route and the new route.

19
00:01:09,090 --> 00:01:13,740
We don't want the user to be able to see the form if they're not logged in and we definitely don't want

20
00:01:13,740 --> 00:01:16,570
them to be able to actually create one if they're not logged in.

21
00:01:16,800 --> 00:01:20,990
And really there's no way most users would ever get to this point if they're not using the form.

22
00:01:21,150 --> 00:01:26,340
But technically someone could send a request from postman or something and still add something to our

23
00:01:26,340 --> 00:01:27,160
database.

24
00:01:27,240 --> 00:01:32,700
If they're not logged in so we want to protect the post route and forget route and this is by far the

25
00:01:32,700 --> 00:01:33,740
more important one.

26
00:01:33,870 --> 00:01:35,970
As far as most of our users are concerned.

27
00:01:36,180 --> 00:01:41,880
But both of them we want to make sure you have to be signed in to Access and we've already defined that

28
00:01:41,880 --> 00:01:47,000
code is logged in the middle where and I'm just going to copy it over.

29
00:01:47,590 --> 00:01:50,480
That's from comments yes or from indexed.

30
00:01:50,510 --> 00:01:59,190
Yes both of them have is logged in and will go back now to campground Yes and just add that at the bottom

31
00:02:00,240 --> 00:02:02,820
and then we'll use that in those wraps.

32
00:02:03,210 --> 00:02:15,360
So here instead of new one to make sure you are logged in just like that and same thing here is logged

33
00:02:15,870 --> 00:02:18,560
in and travel safe.

34
00:02:18,570 --> 00:02:19,240
Great.

35
00:02:19,350 --> 00:02:21,890
Let's fire up the server and give it a shot.

36
00:02:22,280 --> 00:02:23,040
Node.

37
00:02:23,160 --> 00:02:29,600
Yes go to slash campgrounds and we have no campgrounds right now which is fine it doesn't matter.

38
00:02:29,760 --> 00:02:35,580
All we want to do is try and add one and I'm not logged in right now so I should see the log in Forum

39
00:02:35,910 --> 00:02:43,260
which is working just fine because if this is logged in Middleware it's redirected me to slash like

40
00:02:43,260 --> 00:02:52,020
it now but sign up and I'll sign up here as carrot.

41
00:02:52,050 --> 00:02:56,120
Now let's try and add a new campground and I can now view the forum.

42
00:02:56,310 --> 00:02:58,110
Excellent.

43
00:02:58,170 --> 00:03:03,300
Now let's get to the more difficult slash fun part which is associating the currently logged in user

44
00:03:03,750 --> 00:03:05,700
with this new campground.

45
00:03:05,910 --> 00:03:08,910
So in this case if I created camp Karnit and hit submit.

46
00:03:09,060 --> 00:03:11,070
Right now it's just created on its own.

47
00:03:11,070 --> 00:03:17,100
It has nothing to do with any user data but we want to save Carrot's username and Carrot's ID to that

48
00:03:17,100 --> 00:03:18,180
campground.

49
00:03:18,660 --> 00:03:22,580
The way that will accomplish that is pretty much the same way we did it for comments.

50
00:03:22,590 --> 00:03:26,710
We'll have to alter the campground schema to start and before we go any further.

51
00:03:26,820 --> 00:03:31,890
I forgot to mention at the beginning that I am in v 9 now so if you are following along these changes

52
00:03:31,890 --> 00:03:33,540
are in version 9.

53
00:03:33,570 --> 00:03:36,690
All right so let's open up the models that we need.

54
00:03:36,690 --> 00:03:38,610
Which is just campground.

55
00:03:38,680 --> 00:03:46,280
J Yes and we'll add in basically the same thing we added to comment where we now have author and we

56
00:03:46,280 --> 00:03:47,700
called this user author.

57
00:03:47,700 --> 00:03:48,690
Doesn't really matter.

58
00:03:48,690 --> 00:03:55,740
I'll just go with author to be consistent and instead of author we have an ID which is an object and

59
00:03:55,740 --> 00:04:03,720
then we have username which is a string and then Id has type which is that long thing mongoose that

60
00:04:03,960 --> 00:04:14,520
schema that types that object id and then the reference is going to be user just like that and will

61
00:04:14,520 --> 00:04:15,030
save.

62
00:04:15,030 --> 00:04:16,530
We need a comma here.

63
00:04:16,870 --> 00:04:18,880
Let's see we need a comma here as well.

64
00:04:19,200 --> 00:04:25,620
OK so we save now what we'll need to do is set up the logic instead of our campground create.

65
00:04:25,650 --> 00:04:31,740
So when a campground is created we want to take the current user's ID and the current user's username

66
00:04:31,800 --> 00:04:36,570
and save them into the author on that campground and that will work pretty much the same way that it

67
00:04:36,570 --> 00:04:38,380
worked for comment as well.

68
00:04:38,430 --> 00:04:44,630
So let's go to the campground route and then we need to find create which is right here.

69
00:04:44,770 --> 00:04:46,610
It Up Post slash.

70
00:04:46,710 --> 00:04:52,140
Which remember is actually slash campgrounds because actually yes we have this line here.

71
00:04:52,290 --> 00:04:57,480
So when this route comes in are grabbing the data name image description or making a new campground

72
00:04:57,510 --> 00:04:58,090
there.

73
00:04:58,920 --> 00:05:07,140
So before we actually edit this data let me do another one of those conc about logs of request dot user

74
00:05:08,010 --> 00:05:12,900
and this should be review from a last video request that user contains the information about the currently

75
00:05:12,900 --> 00:05:13,980
logged in user.

76
00:05:14,310 --> 00:05:19,710
And if there is no currently logged in user it will be empty except there's no way we can get to this

77
00:05:19,710 --> 00:05:24,490
point unless someone's logged in because of this is logged in Middleware.

78
00:05:24,850 --> 00:05:25,110
OK.

79
00:05:25,110 --> 00:05:28,370
So let's save and we should expect to see request that user.

80
00:05:28,500 --> 00:05:33,450
So Control-C restarted and this happens when we create a campground.

81
00:05:33,900 --> 00:05:35,170
Now let's go and test it out.

82
00:05:35,370 --> 00:05:43,350
Create a campground have an image here and the campground name will be desert the desert and description

83
00:05:43,610 --> 00:05:52,890
will just be hot dry desert we like that and will submit.

84
00:05:53,730 --> 00:05:55,530
And I do need to be logged in first.

85
00:05:55,530 --> 00:06:02,020
All right so let's log in his carit password is password.

86
00:06:02,400 --> 00:06:11,640
Now it's at a new campground again paste that image in desert the desert hot dry desert we now create

87
00:06:11,640 --> 00:06:13,880
a campground and that works fine.

88
00:06:14,340 --> 00:06:18,000
But let's take a look at our council and see if our user it was printed out.

89
00:06:18,120 --> 00:06:20,350
And as you can see it was right here.

90
00:06:20,430 --> 00:06:26,300
So we have a username Khairat ID 5 6 2 blah blah blah the hexadecimal ID.

91
00:06:26,690 --> 00:06:27,330
OK.

92
00:06:27,540 --> 00:06:32,880
Now we want to take that data and save it into new campground and we could do it like this where we

93
00:06:32,880 --> 00:06:42,450
say a new campground author ID equals and then new campground author user name equals but I'm not going

94
00:06:42,450 --> 00:06:43,610
to do it that way.

95
00:06:43,920 --> 00:06:49,230
I want to show you a slightly cleaner way in my opinion which is we're going to make an object here

96
00:06:49,230 --> 00:06:59,540
called var author and instead of that we're going to have ID and username and ID will be request at

97
00:06:59,540 --> 00:07:02,070
user dot unders for ID.

98
00:07:02,150 --> 00:07:10,120
This right here and user name will be request to use her username and we need a comma.

99
00:07:10,340 --> 00:07:14,440
You can see it's complaining which is nice that we get that we'll save.

100
00:07:14,790 --> 00:07:20,110
And then we just need to pass this author into the new campground when we're creating it so we can just

101
00:07:20,110 --> 00:07:23,480
do that like that right there.

102
00:07:23,490 --> 00:07:25,470
And then we don't have to do anything else.

103
00:07:25,500 --> 00:07:29,600
We were already passing in new campground to our DOT create.

104
00:07:29,680 --> 00:07:30,910
So let me get rid of this now.

105
00:07:31,000 --> 00:07:34,470
Cost about LOEG We're now adding in that user data.

106
00:07:34,800 --> 00:07:35,960
And we should be good.

107
00:07:36,000 --> 00:07:39,260
So let's see how it goes and to make sure that it works.

108
00:07:39,720 --> 00:07:47,040
But still a constant log here of that campground which we're calling newly created that's what's coming

109
00:07:47,040 --> 00:07:52,080
back from the database and hopefully we see our author data in there as well.

110
00:07:52,380 --> 00:08:01,610
So restart make yet another campground and I will have to log in which is always frustrating to do and

111
00:08:01,660 --> 00:08:07,470
screencast but it's what we're actually testing here so I have to there's no way around that password

112
00:08:07,470 --> 00:08:09,160
.

113
00:08:09,390 --> 00:08:12,060
Now we're logged in now will add a new campground.

114
00:08:12,060 --> 00:08:19,750
We'll use the same image you Arel and we'll call this one desert to jibberish submit.

115
00:08:20,190 --> 00:08:20,990
OK.

116
00:08:21,330 --> 00:08:23,170
Now let's see what we get here.

117
00:08:23,790 --> 00:08:24,630
Perfect.

118
00:08:24,630 --> 00:08:28,440
You can see that we have comments is empty the description.

119
00:08:28,440 --> 00:08:36,810
Image the name and then author which has user name carrot and Id 5:6 to blah blah blah that whole hexadecimal

120
00:08:36,810 --> 00:08:37,580
idea.

121
00:08:38,040 --> 00:08:38,370
Perfect.

122
00:08:38,370 --> 00:08:40,600
So that's working exactly how it should.

123
00:08:40,650 --> 00:08:44,400
The very last thing that we can do is display that on the show page.

124
00:08:44,400 --> 00:08:50,910
So if I go to one of these show pages we could just have it print out right here submitted by code submitted

125
00:08:50,910 --> 00:08:53,770
by resti or submitted by potato.

126
00:08:53,780 --> 00:08:55,120
So let's do that.

127
00:08:55,140 --> 00:08:56,620
That's our show template.

128
00:08:56,820 --> 00:09:06,060
So we want to open that up instead of use campgrounds show and then we have access to that because it's

129
00:09:06,090 --> 00:09:13,080
inside of campground author author user name and all just do that by adding a other paragraph in here

130
00:09:13,770 --> 00:09:18,280
and then inside we need our E.J. Aztec's and we want campground.

131
00:09:18,410 --> 00:09:24,700
Dot author dot username which is this right here this whole thing is campground.

132
00:09:24,870 --> 00:09:28,190
You want dot author dot username carrot.

133
00:09:28,320 --> 00:09:35,330
And rather than just printing out it let's do submitted by and then carat.

134
00:09:35,790 --> 00:09:39,660
And let's also make this italicised and I can do that.

135
00:09:39,780 --> 00:09:42,800
Let's indent at some space here.

136
00:09:43,410 --> 00:09:44,040
There we go.

137
00:09:44,160 --> 00:09:50,750
And then I'll put E-M tags around this which remember our for emphasis.

138
00:09:50,790 --> 00:09:52,810
Technically it's not about italicised.

139
00:09:52,830 --> 00:09:56,920
It's supposed to be something that is meaningful or that's emphasized on our page.

140
00:09:57,060 --> 00:09:59,640
And it just happens to be italicised by default.

141
00:10:00,090 --> 00:10:06,000
So let's save restart the server and we won't have to log in this time to see if it worked for I just

142
00:10:06,000 --> 00:10:07,180
refresh the page.

143
00:10:07,470 --> 00:10:10,730
We get submitted by carot excellent.

144
00:10:10,920 --> 00:10:12,050
Same thing here.

145
00:10:12,180 --> 00:10:14,600
We don't actually have the author saved to that one.

146
00:10:14,700 --> 00:10:16,010
So let's make a new one.

147
00:10:16,020 --> 00:10:20,780
Sign up again and our username here will be rocket man password.

148
00:10:20,820 --> 00:10:23,020
Once again it's just password.

149
00:10:23,020 --> 00:10:27,390
Let's sign up and then add a new campground and just make sure it works.

150
00:10:27,420 --> 00:10:34,650
So we use the same image and this one will just be blah blah and description is also blah blah.

151
00:10:35,100 --> 00:10:39,960
Let's submit now and let's take a look at it.

152
00:10:39,960 --> 00:10:40,530
There we go.

153
00:10:40,530 --> 00:10:42,810
Submitted by rocket man.

154
00:10:42,810 --> 00:10:43,240
Great.

155
00:10:43,320 --> 00:10:45,880
So that's everything I wanted to get done in this lesson.

156
00:10:45,930 --> 00:10:51,330
I wanted to start by making sure that a user can not create a campground unless they're logged in.

157
00:10:51,370 --> 00:10:56,010
You have to be logged in to actually create a new campground which was a pretty easy fix.

158
00:10:56,010 --> 00:10:58,640
We just had to add in our middleware is logged in.

159
00:10:58,920 --> 00:11:02,490
And then the second thing we did was actually updating the campground schema.

160
00:11:02,520 --> 00:11:08,720
So it now has author with a user name ID and then we just populate that instead of the create.

161
00:11:08,730 --> 00:11:10,070
When you create a new campground.

162
00:11:10,260 --> 00:11:14,730
And then finally we displayed it on the show page campground and author that username
