We'll come back in this video we're basically going to do the same thing we did in the last video with
users and comments but we're going to do it for users and campgrounds.
There are two main ideas.
The first is that we don't want someone submitting a new campground if they're not signed in.
We don't want anyone just to go and create a campground.
Must they have an account.
Anyone can view the campground.
Anyone can read comments but you can't submit a new campground or a new comment unless you're signed
in.
And the second one is not only do we want to make sure you're signed in but we also want to save who
you are your username and ID to the newly created campground just like we did for the comments so that
on a campground show page we can show who created it created by potato created by resti and then that
also has implications later on for who can delete particular comments or campgrounds and we need the
ID and the user name for that to work.
We'll start with the easy part which is preventing someone who's not logged in from creating a campground
.
So we need to open up the campground routes which are an set of routes campgrounds.
J.S. and the important routes here are the Create route and the new route.
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
them to be able to actually create one if they're not logged in.
And really there's no way most users would ever get to this point if they're not using the form.
But technically someone could send a request from postman or something and still add something to our
database.
If they're not logged in so we want to protect the post route and forget route and this is by far the
more important one.
As far as most of our users are concerned.
But both of them we want to make sure you have to be signed in to Access and we've already defined that
code is logged in the middle where and I'm just going to copy it over.
That's from comments yes or from indexed.
Yes both of them have is logged in and will go back now to campground Yes and just add that at the bottom
and then we'll use that in those wraps.
So here instead of new one to make sure you are logged in just like that and same thing here is logged
in and travel safe.
Great.
Let's fire up the server and give it a shot.
Node.
Yes go to slash campgrounds and we have no campgrounds right now which is fine it doesn't matter.
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
which is working just fine because if this is logged in Middleware it's redirected me to slash like
it now but sign up and I'll sign up here as carrot.
Now let's try and add a new campground and I can now view the forum.
Excellent.
Now let's get to the more difficult slash fun part which is associating the currently logged in user
with this new campground.
So in this case if I created camp Karnit and hit submit.
Right now it's just created on its own.
It has nothing to do with any user data but we want to save Carrot's username and Carrot's ID to that
campground.
The way that will accomplish that is pretty much the same way we did it for comments.
We'll have to alter the campground schema to start and before we go any further.
I forgot to mention at the beginning that I am in v 9 now so if you are following along these changes
are in version 9.
All right so let's open up the models that we need.
Which is just campground.
J Yes and we'll add in basically the same thing we added to comment where we now have author and we
called this user author.
Doesn't really matter.
I'll just go with author to be consistent and instead of author we have an ID which is an object and
then we have username which is a string and then Id has type which is that long thing mongoose that
schema that types that object id and then the reference is going to be user just like that and will
save.
We need a comma here.
Let's see we need a comma here as well.
OK so we save now what we'll need to do is set up the logic instead of our campground create.
So when a campground is created we want to take the current user's ID and the current user's username
and save them into the author on that campground and that will work pretty much the same way that it
worked for comment as well.
So let's go to the campground route and then we need to find create which is right here.
It Up Post slash.
Which remember is actually slash campgrounds because actually yes we have this line here.
So when this route comes in are grabbing the data name image description or making a new campground
there.
So before we actually edit this data let me do another one of those conc about logs of request dot user
and this should be review from a last video request that user contains the information about the currently
logged in user.
And if there is no currently logged in user it will be empty except there's no way we can get to this
point unless someone's logged in because of this is logged in Middleware.
OK.
So let's save and we should expect to see request that user.
So Control-C restarted and this happens when we create a campground.
Now let's go and test it out.
Create a campground have an image here and the campground name will be desert the desert and description
will just be hot dry desert we like that and will submit.
And I do need to be logged in first.
All right so let's log in his carit password is password.
Now it's at a new campground again paste that image in desert the desert hot dry desert we now create
a campground and that works fine.
But let's take a look at our council and see if our user it was printed out.
And as you can see it was right here.
So we have a username Khairat ID 5 6 2 blah blah blah the hexadecimal ID.
OK.
Now we want to take that data and save it into new campground and we could do it like this where we
say a new campground author ID equals and then new campground author user name equals but I'm not going
to do it that way.
I want to show you a slightly cleaner way in my opinion which is we're going to make an object here
called var author and instead of that we're going to have ID and username and ID will be request at
user dot unders for ID.
This right here and user name will be request to use her username and we need a comma.
You can see it's complaining which is nice that we get that we'll save.
And then we just need to pass this author into the new campground when we're creating it so we can just
do that like that right there.
And then we don't have to do anything else.
We were already passing in new campground to our DOT create.
So let me get rid of this now.
Cost about LOEG We're now adding in that user data.
And we should be good.
So let's see how it goes and to make sure that it works.
But still a constant log here of that campground which we're calling newly created that's what's coming
back from the database and hopefully we see our author data in there as well.
So restart make yet another campground and I will have to log in which is always frustrating to do and
screencast but it's what we're actually testing here so I have to there's no way around that password
.
Now we're logged in now will add a new campground.
We'll use the same image you Arel and we'll call this one desert to jibberish submit.
OK.
Now let's see what we get here.
Perfect.
You can see that we have comments is empty the description.
Image the name and then author which has user name carrot and Id 5:6 to blah blah blah that whole hexadecimal
idea.
Perfect.
So that's working exactly how it should.
The very last thing that we can do is display that on the show page.
So if I go to one of these show pages we could just have it print out right here submitted by code submitted
by resti or submitted by potato.
So let's do that.
That's our show template.
So we want to open that up instead of use campgrounds show and then we have access to that because it's
inside of campground author author user name and all just do that by adding a other paragraph in here
and then inside we need our E.J. Aztec's and we want campground.
Dot author dot username which is this right here this whole thing is campground.
You want dot author dot username carrot.
And rather than just printing out it let's do submitted by and then carat.
And let's also make this italicised and I can do that.
Let's indent at some space here.
There we go.
And then I'll put E-M tags around this which remember our for emphasis.
Technically it's not about italicised.
It's supposed to be something that is meaningful or that's emphasized on our page.
And it just happens to be italicised by default.
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
refresh the page.
We get submitted by carot excellent.
Same thing here.
We don't actually have the author saved to that one.
So let's make a new one.
Sign up again and our username here will be rocket man password.
Once again it's just password.
Let's sign up and then add a new campground and just make sure it works.
So we use the same image and this one will just be blah blah and description is also blah blah.
Let's submit now and let's take a look at it.
There we go.
Submitted by rocket man.
Great.
So that's everything I wanted to get done in this lesson.
I wanted to start by making sure that a user can not create a campground unless they're logged in.
You have to be logged in to actually create a new campground which was a pretty easy fix.
We just had to add in our middleware is logged in.
And then the second thing we did was actually updating the campground schema.
So it now has author with a user name ID and then we just populate that instead of the create.
When you create a new campground.
And then finally we displayed it on the show page campground and author that username