OK so the last thing that I want to do is refactor the middleware.
So what that means is basically right now in a campground route we have check campground ownership and
we have is logged in and we're duplicating is logged in over here where we have is logged in for comments
.
And then again check comment ownership.
So we're just going to refactor all of this move those methods into a new file.
So I'm actually going to make a directory in the top level of our app make directory middleware and
then inside of there touch middleware.
And I'm just going to combine all the middleware into one file so we could have campground middleware
and then we could have comment middleware but can it make one file because we don't have that many.
And I'm going to call it indexed.
J.S. and I'll show you why rather than calling it middleware.
Yes I'm calling it index inside the middleware directory.
Now let's open that up.
And then in here all the middleware goes here and then we also need to add in our module that exports
equal to something.
So we need some objects.
And I've shown you a few different ways of doing this.
We're going to call a variable called middleware object that doesn't exist yet to obey J.
And that will contain all of the methods.
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
lines that look like this middleware object dot and then we'll have check campground ownership equals
a function and then we'll have middleware objects dot check comment ownership equals a function as well
and so on.
So we're adding all of these functions onto middleware object and then we're setting modules that exports
to be middleware object.
The other way that I've shown this to you is just like this where instead of here we just add in check
campground ownership as a function which is also fine.
But I just want to show you another syntax rather than defining all of the functions at once when we
define the object.
We can do it after the fact and then the other way that you'll see it occasionally.
It's just like this where there is no variable.
You just have an object right here.
And then all of your data goes in there.
So all of those will work.
We'll do middleware object to find that as an object and then we'll add in our code.
So we start middleware object dot check campground ownership equals the giant function and we'll go
ahead and copy that from campground Yes or campground just copy this whole thing over we'll cut it out
and we will have to make a change or two because we don't need to define that name again but we do need
those arguments that I care of one too many curly braces as well.
That's outdone this little ever go.
Looks good.
We'll do the exact same thing this time.
Check ups check comments.
And let's go copy that right here.
Cut it out as well.
Pay set in
and you want function request response and next out this as well.
Either way.
Great.
And then we have our last one which is is logged in.
So cut it out of here and I'll cut it out of here as well it's identical.
OK.
Now let's go to our middleware and just add that in to get at the bottom middle where object is logged
in equals function request response next.
Same code.
We just moved it to this new file and we added everything into an object's middleware object that were
then much of that exporting at the end.
Great.
Now we need to require this file index such as inside of the middleware directory.
So let's start with campground and up top we'll just require middleware and to do that we need to first
figure out based off of where we are which is instead of routes campground.
Yes how do we get to index that address instead of middleware.
Well we need to back out one directory up into the 10 and then we need to go in to middleware.
So we need a VAR we'll call it middleware equals require and it's a dot dot slash.
So that gets us into the 10 slash middleware and we could do this index.
Yes.
But the reason that I wanted to show you I want us to name it indexed us is that there is actually a
special name that if we require a directory but not a file if I just require middleware it will automatically
require the contents of index Dot.
Yes that's supposed to be like the home where the main file where other things are required.
So actually if we take a look in the node modules directory where are you inside.
There we go.
Let's take a look at Express.
You can see there's an index such as file and index that Julius is the one file that is actually required
when we require the Express directory and it requires a bunch of other files in turn.
And those files might require a bunch of others.
But that's really important.
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
sized middleware.
Now we need to use middleware because none of these functions are defined is logged in
check campground ownership.
Those don't exist.
They're all inside of the middleware objects now just like that middleware dot is logged in mean the
same thing for is logged in here and here and here.
And finally there we'll save.
And we need to do the same thing in our comments file.
There we go.
And we just want middleware.
Dot is right in the middle where Dot is logged in.
Same thing for check common ownership check common ownership.
And one more check comment ownership.
So what we've done is two things.
One we've really cleaned up the route's files a little bit.
They don't have those extra middleware at the bottom which are pretty long actually.
If you take a look.
But we also are no longer writing this code twice is logged in and any other future middleware we may
need that we're going to share in different files.
We don't have duplicate.
We just add them to this file.
All right.
Let's make sure it works.
As a final test.
No problems here which is good
and everything looks OK but we actually have a problem.
If I clicked delete this illustrates it tells me campground is not defined inside of middleware slash
index such as Line 6 middleware object at check campground ownership.
If we take a look inside of our middleware file check campground ownership tells me campground wasn't
defined a month 6.
What I need to do is require campground.
So far campground based off of where we are inside of the middleware directory technically of the 10
I need to do require dot dot slash models slash campgrounds and then duplicate it and do the same thing
for comments.
Require that Slash model slash comments save once again restart
go back refresh log in again.
Sirius Black.
Now if I try and delete this everything works fine.
And dang it.
Now I'm down another campground.
Oh the things I do for this class.
OK so just double check and it still works as well.
There we go.
So what was happening is that we didn't require the correct dependencies and we didn't know that until
we actually tried to run the code once required campground to comment.
Everything is good to go.
All right so all we really did there was move some code out into a separate file and require that file
but it cleaned up our routes a little bit and it's also nice because now we have other middleware that
we need to add in.
We have a little structure set up a place to put them and it's very easy to require them all over the
place.
Awesome.
So that wraps up full crud for a campground in comments.
You can now create read update and delete campgrounds and you can create read update and delete comments
as well and those permissions in there so there's authorization and authentication so you can't create
something you for not logged in.
You can't edit something or delete something if you're not logged in and if it doesn't belong to you
but you can read a comment or you can read a campground if you're logged in or not it doesn't matter
.
Same thing for comments.
So we have three kind of levels of permissions.
One is you can do any.
You can read something for not logged in to is you can create something if you are logged in doesn't
matter who you are though.
In theory you can edit and update something but it does depend who you are.
Next up we're going to add in some other fun features and the one that hopefully is the most exciting
or that will at least make the app more usable is something called Flash messages so it's a way to see
an error message that pops up that says successfully created this or you don't have permission to do
that or Please sign in or password incorrect so it makes things a lot more usable.
So we'll see that soon.