Awesome.
So now that we have flash set up correctly we have the bootstrap alerts displaying or not displaying
when they're supposed to be hidden.
But we have things set up now we can just go in and fill in the error or success messages wherever we
see fit.
So let's just take it one route at a time.
So let's start with our campground wraps.
So anyone should be able to go to slash.
There shouldn't be any error messages or success messages if you go to just slash campgrounds the index
.
If you do try and send a post request to slash campground which is create you do have to be logged in
.
So if you're not logged in let's go find the middleware which is instead of middleware index Yes.
And we find is logged in.
We already have this set up.
So rather than Please log in first.
Let's say you need to be logged in to do that.
We'll save that.
Go back to campgrounds.
So that takes care of create.
Same thing to see the form.
The new form you have to be logged in.
We have the same flash that we have here say middleware further down.
Anyone can go to the show page at it.
We have check campground ownership.
So this is going to check if the user who is trying to access the edit form for a campground actually
owns that campground.
So instead of our middleware if we go to check campground ownership which is right here we have a few
things that we can do.
First we're checking is a user logged in.
So if not before we redirect let's do a request dot flash ups error and we'll just do.
You need to be logged in to do that.
OK but this is most likely not going to happen very often that someone even gets to that page because
remember you can't even see the link unless you own something.
So to actually go to the edit page you have to manually enter that you are real.
So it's it's you know really more for testing purposes and to be as secure as possible.
But most users aren't going to be trying to edit you know typing out these long you or else by hand
.
All right.
So the more important part though is once we find the campground if the user is logged in find the campground
.
And then if there's an error we could add something here so this would refer to an error from a database
if it couldn't find that.
So we could do a request dump flash error and just do something like let's just say campground not found
or something like that.
We probably won't see that ever but we can put it there and I'll copy this line.
Now this is the important part.
So if the found campground author ID is equal to the logged in user ID move on.
So no flash there.
If it's not you can say something like.
Permission denied.
But let's just do.
You don't have permission to do that.
There we go.
So if we go back to camp grounds we also have update which is using the same thing.
Check campground ownership and we have destroyed which is also using the same thing.
And we could go in and fill these in so I won't spend time on the video but we could go.
And when we're trying to find a campground by id and update if that campground doesn't exist.
So actually rather than this one let's do it for it.
So if you're trying to edit a campground that doesn't exist we could add in something here.
So we'd check for error and I won't spend time doing that again.
But I do want you to know you can add these in wherever you want so you can add in this request up flash
and add in any message error or success.
So if you wanted to send a congratulations message basically for everything that the user ever did you
could easily do that if you wanted to.
And in fact let's do that.
We'll wait until we get to comments.
So anyway we have destroy again using the same middleware there.
So now let's move on to comments.
So you try and make a new comment.
You have to be logged in.
We already have that done
and the comments create route rather than new you still have to be logged in.
Same thing.
And we could do something like this.
So once you create that comment if there's an error we can do right or request a flash error and just
say something went wrong.
And again and you probably would never see this but if the database was somehow screwed up if something
went wrong with the data then there might be an error when we try to create that comment.
Otherwise though we create the comment and then we redirect.
So right here we could add a message request dot success and we'll just add an Access fully added comment
that's you added or created comment and then we redirect back to the show page then we have added.
And this is checking check comment ownership.
So if we go to our middleware we have check comment ownership rate here so we can do the same thing
if the user is not Lyddon or display one thing here.
You need to be logged in to do that.
If they are logged in if they don't own it will also display an error.
You don't have permission
to do that.
OK.
And there are other places we could add thing to it of course but I just want to keep it simple and
the most important places which are really just errors.
So I don't want to do it for every single thing that you create or edit.
But we could easily you know when you edit something when you delete something which actually destroying
is more common to show a message.
So I can we can do that so let's say when you delete a comment which is at the bottom here right here
we'll add in a success.
Comments deleted.
So this is once it's been removed.
If there's an error we could add something else but I won't if there's not an error.
Flash this message then redirect.
All right a last bit of routes we have are the authentication routes.
So first things first let's see when you register.
You don't want anything to show up there right away when you send the post requests to slash register
though when there's an error.
We do want to add something in.
So this would be if you can't sign up for some reason if your password is empty if the username is empty
if the username is already taken that would all happen here.
And we don't have to write those errors on our own.
That's what this area refers to right here.
So let me just show you that first.
If we do request a flush error and then rather than campground not found as a string we just add in
VRR Remember we're trying to register a user pass in new user and the password from the body.
Then there's a callback error and user error checking if there's an error.
And this is coming from passport.
It will be empty or there will be an error there.
And if that error exists it will be something like password can't be blank or a user name it can be
blank or username is already taken.
And we're just sending that through so we don't have to write those messages ourselves.
We could write our own custom versions but for simplicity's sake we're just going to take advantage
of what's already done for us and we can do the same thing once you do register and then we log you
in.
We can have request up flash success and we'll just say welcome to yo camp and then we'll add in the
user name and the user name we access from different places we can just do user dot username.
It would also be request a body to use your name but we'll use the one coming back from the database
just in case somehow it got changed by our database.
All right.
A lot of different places we can add messages in.
Let's just check again if there's any other important part.
So when you log out we already have that.
And we should be good to go while we're here.
We can get rid of our middleware because we've now moved that to the middleware file.
OK.
So let's give this a shot.
Restart the server and see what happens.
So we start by refreshing.
If I try and add a new campground tells me I need to be logged in.
Let's go back.
Same thing if I try and add a comment I need to be logged in.
All right.
So now let's sign in or sign up and let's try something like signing up as Rustie who already exists
here.
And never mind.
Apparently Rusty did not exist in this version of the app.
We have so many different versions databases going on.
So it tells me welcome to your camp Rusty.
Now if I do log out I also get logged you out.
Now if I try and sign up as Rusty that's already taken
they'll see that I get this object.
Object here.
So let's take a closer look at that in just a moment.
Object object when I sign up and I get a sign up error that will come back to that.
Let's take a look at some of the other errors that we saw.
So if I do log in as Rusty again
and now I try and go to samen creek and this I try and but say at this campground which the link isn't
even here but if I went to slash at it it tells me you don't have permission to do that.
So all those are working except for the error around trying to sign up as resti.
Once Rusty has already been created.
So I did mention instead of our.
It's kinda confusing.
We have to index files.
So let me close out the index middleware and we'll take a look at our index wraps which is where we
have.
Let me just show you over here just so it doesn't get too confusing instead of routes number campgrounds
comments and we have index.
So instead of index where we have this line right here
request that flash error and we're passing error and let's just do a simple con. The log VRR and let's
take a look at what what we get.
So to cause that error again.
So let's see what happens if I try and sign up with an empty username empty password shouldn't work
.
If we take a look at what we get here a message.
So this is our.
And inside of it there is name.
And more importantly message no user name was given.
So rather than just passing through here are we want your message
just like that.
So restart the server now and whenever there's an error message it will be different.
So in this case no user name was given.
It could also be no password is given user name already taken as you can see I got this earlier.
So let me try this again.
Register just refresh make sure let's leave it blank and I get no username was given.
Now if I try and sign up as resti which already exists I now get a user with the given name is already
registered.
So we're taking advantage of that E-R our message in passing that through.
And these are we're not finding these messages are coming back from Passport actually from mongoose
but because we have that mongoose passport local package that we're using.
So we're getting that automatically.
All right.
So it's kind of a lot of work as far as the number of videos there was three videos to get this done
.
But compared to doing this from scratch adding this into every request and dealing with all of that
it's much easier