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