Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Navigation items based on user's session
0:00 I'm actually logged in, can you tell?
0:03 Of course you can tell, it says login and register.
0:05 Normal science once you're logged in
0:07 it says here's your account, logout, sign out
0:09 something like that.
0:10 But you can tell that I actually am logged in if I go here.
0:14 So, what gives?
0:15 Well, we need to control this navigation based
0:18 on whether or not we're logged in
0:20 and luckily we have one and only place we got to do that
0:22 over in our little shared layout here.
0:24 So, up here we have these bits
0:26 that have to do with navigation
0:28 and account and so on
0:29 and so, let's go actually over here
0:32 and let's say if user_id and if.
0:39 Okay, these all look pretty good
0:41 and up here we want to have
0:42 if you have a user_id
0:44 it's going to have your account.
0:47 I also have a logout.
0:49 And that'll just be /logout.
0:52 All right, so theoretically this'll work.
0:54 Let's refresh it and see.
0:56 Now why is this not changing?
0:58 It's not changing because here's the bad part
1:02 every single view method
1:03 has to send the user_id across.
1:06 Super annoying.
1:07 What we're going to see in the next chapter
1:09 is some really awesome patterns
1:10 that will make this go away.
1:11 Let me fix it the hard way now.
1:13 We'll see that it works
1:14 and then we'll refactor it to a pattern
1:16 that you can just appreciate more
1:17 because it also solves this problem.
1:20 So, what we got to do
1:21 is all the places, when I go in here
1:23 let's just work on the home, for example
1:26 down here in the views.
1:29 In addition to all this stuff
1:30 we have to set user_id
1:32 to be cookie auth.
1:39 Get user and I say flask.request, lowercase request.
1:45 Like so, so we got to put that everywhere.
1:47 We got to put it here, we got to put it into about
1:49 and there's no need for you to watch me write this
1:52 a bunch of times.
1:53 So, I'm going to go do that on every single view method
1:54 and then we'll come back.
1:56 All right, I put it in every single view method
1:58 and I'm going to refresh this page.
2:00 Now every view method passes back the _user_id
2:03 if it's there and none if it's not.
2:05 So, let's refresh the page and see what happens.
2:08 Whoa, account, all right.
2:09 Awesome, account is there, welcome to your account, Michael.
2:12 I can go all over, I come over here
2:13 even go to one of the packages
2:14 you can see account and log out
2:16 so we can go to our account
2:17 and if I logout, I'm now logged out
2:20 I can still cruise around the site
2:22 can see this is keeping track of what I'm doing, right?
2:25 If I try to go to account
2:26 still it's going to redirect me.
2:28 Right, so that's working super well.
2:29 It was actually really easy to do
2:31 just tiresome that we got to put it everywhere
2:34 and if it's not there
2:35 then that page will just basically think we're logged out.
2:37 Like I said, we're going to work
2:38 on a pattern called view models
2:40 that just completely solves this problem
2:41 for the entire site.
2:43 For now, I've got to do this more manual
2:46 but you can see it's not super hard, right?
2:47 I log in.
2:49 Boom, navigation changes right up there across the site.