Welcome back in this lesson which is part four of adding an occasion to your camp we're going to add
the log out logic.
We're also going to prevent anyone from accessing the new comment form without being signed in.
And in the second part of the video we're going to focus on the nav bar.
Right now we have an app bar.
These links don't work.
So the first step is to make them work.
And then the second feature is to only show the necessary links at any given time.
So we should never see log in sign up and log out if we're not signed in.
We should see log in and sign up but not log out.
And if we are signed in we only want to see log out.
So we'll be implementing that logic at the end of the video to start.
Let's add our log out route.
So the way that log out works is that it's just a GET request.
So we'll add log out route and in here and after I get slash log out function of our request no response
and all we have to do is use the request log out that we get for free.
So we didn't have to write this ourselves.
This comes from the packages we have installed and that's it.
However we do want to redirect somewhere so let's redirect to slash campgrounds and that's it.
We now have our log out rather nice and easy.
Now let's open up the nav bar and that's inside of views Parshall's.
And then that's in the header and what we want to do is just make these links go to the right routes
.
So log in is log in sign up is slash register and log out slash log out.
And that start the server no errors refresh the page.
Let's make sure these work so I can log in and I log in Severus with password as my password.
And you'll notice we don't have enough bar here so we should do that next.
I didn't get that far but now I should be able to log out and if it works I should be redirected back
to the campgrounds page.
So before you forget let's go and add the layout files the header and footer Parshall's to the log in
and register forms.
So stop the server and let's open up another file.
Let's just open up the campground index.
So C9 views slash campgrounds slash index and we'll just take this and steal it and we'll put that on
the log in form up top and we won't actually style anything yet with bootstrap and then we'll do the
same thing with the footer.
And if we run this right now we try and visit that log in form.
We have a problem and the problem is that we use the exact same path dot dot slash Parshall's slash
Hetter that we used inside of the index that Ejay us for campgrounds.
But we're not inside that directory.
So let me show you what I mean instead of campgrounds to access the Parshall's and the header file.
We have to go back one level and then into the Parshall's directory and then into the header W.J. X-File
.
But where we are right now on our log in form we're not inside of a separate directory.
All we have to do is dot slash Parshall's dot sized Parshall's again restart and refresh.
Now we get to that far up here.
Let's copy this over to register
and the footer as well and we'll save and we should now have those forms on both.
So I can go to sign up log in and if I click log out right now it will lock me up.
Excellent.
So the next thing I want to focus on is not letting a user add a comment if the user is not logged in
.
So right now I'm not logged in.
I shouldn't be able to see this form at all.
And what we want to happen is if a user clicks this button an unauthenticated user clicks it he or she
should see the log in form and if the user is logged in then he or she should be able to add a comment
.
So let's do that now.
So we need to go back to our address and this is where we need to add in a middleware and all just to
find it at the very bottom is logged in.
And if you want to review of how this works in depth go back to the last video of the last unit where
I talk more about it.
But a middle where we need those three request response and next which is the function that is going
to be called after this middleware and then we'll check if a request is authenticated.
If that's the case then we'll just move on to the next thing which in our case would be moving on to
rendering the new campground or the new comment form or otherwise redirect to log in.
So rest redirect slash log it and then we can use this wherever we want.
So if we wanted to use that to be signed in to access any page at all you would just put is logged in
in whatever routes you want to use it in.
And in our case going to go up to the comments we create a new comment route that shows the form.
We just need to put that right there.
So when a user makes a request to the page or the form comments slash new it will run is logged in first
and that will check if the user is logged in if the user is logged in.
It calls next which we'll just call this code which will end up with us seeing the comments form if
the user is not logged in.
Then we redirect to slash logging.
Let's give it a shot make sure we don't have any syntax errors.
Let's go back here and we'll go to the home page for now or to the campgrounds page refresh.
Now let's try adding a comment to desert Mesa add new comment and it takes me to log in one more time
and you come in and takes me to log in.
But now if I do log in I'll do several again and I log in.
I can now add a comment if I go back and click add new comment.
I can now view the add new comment form.
Excellent.
There is one small issue which is that we're not actually protecting the route that actually adds the
comment in.
So that's this one here which is the post after post campground slash ID slash comments which is where
the form goes.
Right now we're just hiding the form from a user.
We're not letting a user see the form.
If the user is not logged in and that's great theoretically someone could still send a post request
if I fired up postman and I sent a post request right now to this you are l to a comment create route
it would create a comment so let's add our is logged in here as well and that will just prevent anyone
from adding a comment unless they're are logged in.
And if they're not logged in it will just redirect back to slash log in just like we had before.
Let's restart again.
OK.
Let's go back to campgrounds are fresh.
OK now let's try adding a new comment.
I'm not logged in.
That's good.
Now Severus password.
Now let's add a comment again.
This time I'll say this is great.
Yes wonderful comment and author will be Severus and very soon we won't actually have to explicitly
type this we'll just grab it from the user information click submit and there's my comment.
Excellent.
So that's all we need to do there to prevent a user from adding a comment if he or she is not logged
in