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 0:00 if the cookie's username key isn't set. 0:05 Because we want this behavior to happen when we visit the index route. 0:08 The route handler that handles the root route, is where we need to add the logic. 0:12 Here in the app.js in the index route, 0:21 I made an if statement checking the value of name. 0:24 If it exists, we want to render the index template, 0:29 otherwise redirect to the hello route. 0:34 Now I want to present you with another challenge, 0:37 can you add similar logic to the hello route? 0:40 We want to redirect the user to the index page if the cookies username value is set. 0:44 Otherwise, we went to render the hello form. 0:50 Go ahead and pause this video and see if you can add that code in now. 0:53 I'll show you what I did. 1:00 As you can see the code is very similar. 1:04 I declared the name here, then I checked whether if the name was present or not. 1:08 If it was present, I redirect it to the index route. 1:16 Otherwise I rendered the hello form. 1:21 We can test some of this code very easily. 1:25 We just need to fill in the form and set the cookie, but 1:28 still we don't have an easy way to unset the cookie value 1:32 without going to DevTools and doing it manually. 1:37 If we had a goodbye button to unset the cookie, testing would be a snap. 1:42 I want to offer you a bigger challenge to really test the skills you've 1:49 been learning. 1:52 Your challenge is to implement a goodbye button for the app. 1:53 Don't worry, you already know all the pieces that you need to do this, 1:57 except for one, clearing the cookie. 2:01 Something we haven't gone over yet, but 2:03 see if you can find out how to do that in the expressed documentation. 2:05 If you get really stuck, I've included a link to the method that you'll need 2:09 in the teacher's notes, so you can get right to it. 2:13 Let's go over the steps you'll need to take, 2:17 add a form with a'goodbye button to the Welcome page. 2:20 Create a new route called goodbye for the phone to post to. 2:24 Clear the cookie in the new route, then redirect the user to the hello form. 2:29
You need to sign up for Treehouse in order to download course files.Sign up