Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Put some of what you've learned about redirects to practice.
Steps to implement the feature in this challenge
- Add a form with a goodbye button to the welcome page.
- Create a new route called goodbye for the form to post to
- Clear the cookie in the new route
- Redirect the user to the hello form
Clearing a Cookie in Express
In the last video, I challenged you to redirect users to the hello page
if the cookie's username key isn't set.
Because we want this behavior to happen when we visit the index route.
The route handler that handles the root route, is where we need to add the logic.
Here in the app.js in the index route,
I made an if statement checking the value of name.
If it exists, we want to render the index template,
otherwise redirect to the hello route.
Now I want to present you with another challenge,
can you add similar logic to the hello route?
We want to redirect the user to the index page if the cookies username value is set.
Otherwise, we went to render the hello form.
Go ahead and pause this video and see if you can add that code in now.
I'll show you what I did.
As you can see the code is very similar.
I declared the name here, then I checked whether if the name was present or not.
If it was present, I redirect it to the index route.
Otherwise I rendered the hello form.
We can test some of this code very easily.
We just need to fill in the form and set the cookie, but
still we don't have an easy way to unset the cookie value
without going to DevTools and doing it manually.
If we had a goodbye button to unset the cookie, testing would be a snap.
I want to offer you a bigger challenge to really test the skills you've
Your challenge is to implement a goodbye button for the app.
Don't worry, you already know all the pieces that you need to do this,
except for one, clearing the cookie.
Something we haven't gone over yet, but
see if you can find out how to do that in the expressed documentation.
If you get really stuck, I've included a link to the method that you'll need
in the teacher's notes, so you can get right to it.
Let's go over the steps you'll need to take,
add a form with a'goodbye button to the Welcome page.
Create a new route called goodbye for the phone to post to.
Clear the cookie in the new route, then redirect the user to the hello form.
You need to sign up for Treehouse in order to download course files.Sign up