Anvil: Web Apps with Nothing but Python Transcripts
Chapter: User management and authentication
Lecture: Toggling UI state based on logged in user

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we've signed up and we now have users maybe, probably. I'm not entirely sure, right we have no way to tell whether the user's logged in.
0:09 So, let's go and actually toggle a couple of things based on user state. First of all, let's go over here and say, the user is this.
0:16 we'll say, set account stage, or something like that. What we need to do is we need to hide and show some of these elements depending on
0:27 whether they're logged in. And, we'll come in here and we'll say, user. That's great. And, let's go write that down here at the bottom.
0:38 Put a little self, because that's how Python works. And, here we'll say something like this if there is a user, we want to show account and log out.
0:47 If there's not a user, we want to hide those two but then show log in and register. And, let's try to do those in line. That's probably the easiest.
0:53 So, we'll say self link account.visible = user is not None, so we have a user. Probably the easiest way there. And, what other ones do we have?
1:06 We have log out, again. And, then log in, we want that to be the opposite, right user is None. Same thing with register.
1:23 Now, that's getting called here. Let's also call it right at the start. And, we'll do something like this
1:31 we'll say, user, now where we do we get the user from here? There's no log in. We can just ask Anvil, is there a current user.
1:37 If they're logged in, we get them if not, then we get none. Get user, like that. All right, let's go ahead and run this and see what happens.
1:50 set_account_state is not defined because of course self. is what I should have written there. Look at that. We're logged in.
1:59 So, not only did we see the account created in the database when we set that cookie, we are now logged in already and this is great.
2:08 The other thing we need to do is derive this anonymous versus details page. Let's go fix that. That's super-easy as well.
2:17 Now, this is going to happen over in navigation. When we say, navigation go home we're basically going to call this function as well.
2:24 Let's go over to the navigation, go home we're going to get the user, and then we're going to decide if there's a user, I'm going to do something
2:33 else we're going to do this. And, that something that we want to do is going to be the HomeDetailsComponent.
2:40 Here we go. So, when we go home, we have to look. Do you already have this in details or not? Also, when we have things like compare
2:48 or go to your account, we want to make sure that there is a user, and send them along. So, we're going to take care of that validation in a minute
2:54 but let's just see that we're getting the right home screen as well. Spelling is hard. Let's see what's going on here. User.
3:04 Look at that, home logged-in view. Now, it's not so easy to show you the not-logged-in view because there's no log out, right
3:13 that doesn't do anything right now. We'll do that in a minute, but this is really cool. We're deriving this navigation
3:18 and we're deriving the home screen based on whether or not there's a logged-in user. Great.


Talk Python's Mastodon Michael Kennedy's Mastodon