Anvil: Web apps with nothing but Python Transcripts
Chapter: User management and authentication
Lecture: Registering a new user
0:00 With our user service set up
0:01 let's go back to our HomeForm here
0:04 and work on register.
0:06 So if somebody comes in here and clicks register
0:08 we want to show them a form, gather their information
0:11 create an account and then set a cookie potentially
0:13 to remember across at least this session.
0:17 We're going to do that, and then once we have the ability
0:19 to create accounts, we'll be able to later
0:21 log in and log out of those accounts.
0:24 So here's our register, click handler
0:26 and turn out if we go way to the top
0:28 we have anvil.users, and this is the thing
0:31 that we're going to use, to actually interact with it
0:34 so let's go back down here, our register
0:37 and we say anvil.users.signup_with_form.
0:43 You can say sign up with just email, just Google
0:45 or whatever.
0:46 We're just going to say sign up with form
0:48 and we're just going to run it for a second
0:50 and show you something that's kind of annoying.
0:51 We don't need a pass here.
0:53 Once you do this we maybe want to refresh
0:55 and send you back home, say go home.
0:59 Now that we have accounts, what we want to do
1:01 when you go home, is to, let me load up this HomeForm here.
1:05 Let's say that's over here.
1:07 We want to check and determine whether or not
1:10 there is an account.
1:11 If there is an account, show the anonymous component here
1:15 or just hey welcome to our app, you should sign in
1:17 create an account.
1:18 Or, the one that is the details
1:21 hey welcome to your account, here are your details.
1:24 So we're going to deal with that in just a second.
1:25 Let's take it one thing at a time
1:26 but it turns out to be pretty easy.
1:28 So down here, we going to go and register
1:31 and then we're going to go back home.
1:33 Just to refresh the screen here.
1:35 Let's go ahead and run this.
1:37 So we're on our anonymous page
1:38 if we click register, now all of a sudden
1:40 we get this cool signup.
1:42 So we can sign up with an email
1:45 or we can sign up with Google.
1:47 Now, how do I decide that I didn't like that form?
1:52 Huh, well it turns out if for some reason
1:55 you click that and you don't actually want to register
1:58 to bad, you've got to refresh the page.
1:59 So let's go fix that really quick.
2:01 That's kind of annoying but I wanted to show you that
2:03 so we have this allow_cancel is True
2:06 and now if we run it, it's probably the right experience
2:09 that we're looking for. So click here, and sign up with email or with Google
2:12 or you know what, actually never mind, forget it.
2:16 So we're going to go over here and we're going to sign up.
2:19 We just going to sign up with a simple email.
2:22 No problem there, and say remember me.
2:23 So we'll say sign up.
2:26 Give this just a second, and it went home.
2:28 We got some output over here.
2:31 So it automatically created some columns in that table
2:34 which didn't exist before right
2:36 so it basically created that table over in the user
2:38 service, and now you should be logged in.
2:42 Our app didn't understand that.
2:44 It do anything. So, that's okay though.
2:47 We've already signed up.
2:48 Let's see what else we can do here.
2:50 We can also say remember by default, that is true
2:52 but you could disable that if you want.
2:55 Last thing, let's see if our user actually got created here.
2:59 We go to our data tables.
3:00 Look at that, there's a email@example.com.
3:04 This account is enabled, is_pro is unset.
3:07 But check this out, we have a nice password hash here.
3:10 We have the remember login details that have been set
3:14 and the last time that we logged in.
3:17 Super cool huh?
3:18 So really really great to be able to just check
3:20 a few boxes, call a simple function, and have Envol
3:23 handle all the tricky user management stuff for us.