Anvil: Web Apps with Nothing but Python Transcripts
Chapter: User management and authentication
Lecture: Guarding restricted views
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Here's our app running again and we have our user management working beautifully, right? We can log in, log out, register all that good stuff.
0:07
However certain things don't make a lot of sense, right? We can't get to the account view over here because it's not shown, but in order to
0:15
add a measurement, hey we need to log in before we can do that. In order to compare our history against the average person we also need to have us
0:26
there as the source of what our data is. So these two views really don't make a lot of sense until you're logged in.
0:33
What we're going to do is we're going to refactor things just a little bit to make sure that before we're allowed to navigate to those views
0:40
we make sure that there's an account there So that's what we're going to do. So over here, let's go to the bottom and write little function here called
0:50
require_account. And what this is going to do it's going to run and make sure either an account already exists, or already logged in
0:58
or make you log in to do that okay? So let's go in here and say user = data_access.the_user() That's what we had before, we'll say
1:10
if there is a user, return that user, everything is good. But if there's no user we're going to require them to log in.
1:19
We'll say user = anvil.users.login_with_form(allow_cancel=True) Cancel true, what we had. Now maybe this worked maybe it didn't. So we're going to
1:32
get this user and we also need to toggle potentially if they did log in and they weren't something like that, the user states
1:39
so after we get the form like this we'll say form.set_account_state(user) what did we call this over here? Remember we have this set_account_state.
1:50
And pass in, the user and it toggles the UI and then let's return user. So whoever calls this they can check to see that a user
1:59
came back, either here or one was logged in. If there's none, then they can decide. Alright, so let's see, how will we
2:05
use that up here? Let's just get rid of the top navigation. No, this one's fine. If you want to add you're going to need a user, right?
2:13
So let's do that, let's say this. If user =require account, say if not, go home. So if there's no user, they didn't log in
2:27
either the user already logged in or we asked them to log in and they didn't then we're just going to say
2:32
look you can't go here, we're going to take you home and this is probably going to look a lot like that
2:36
so same thing for compare and same thing for account. You can't go to your account if you're not logged in.
2:45
Uh- this looks pretty good. Let's quit and run it. Dyr@theanonymous. We try to go here, it should log us in.
2:53
Great, could log in. Couldn't log in the first time. Boom. We logged in. It let us go through. Next time, we're going to try go to compare
3:05
but we're already logged in so it should just let us go there. Boom. Same for the account. Same for add now.
3:12
If we log out, however, and try to go back to compare boom, either we log in and we cancel oh, that didn't work, did it? Aah looks
3:22
like we're not quite there, I think I might need to return or something like that. So let's go and quickly fix this little bit.
3:31
How about that? Perfect. Let's test our cancel. So we're going to try to go to add, we're not going to log in
3:42
it should take us back to home anonymous, cancel, boom. This time let's try to go to add and actually log in. Took us there, super. Same thing
3:52
should be the case for the others. Right, we go here, everything's fine not, it won't let us go there if we don't log in.
3:59
Great, so, I think our user interaction is working pretty well. I might even call it done.