Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: User input and HTML forms
Lecture: Navigation items based on user's session

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


Talk Python's Mastodon Michael Kennedy's Mastodon