User Management Features

In this tutorial you will incorporate user management features to allow users to:

  • Register for an account

  • Login with a registered account

  • Logout of the website

The events that trigger all these features will be link clicks, so we have to go back to the MainForm Design mode and create handlers for the Register, Login and Logout links.

Handlers

Code

Register

The first feature we need to add is the registration feature so we can create users.

Go to the link_register_click handler. Delete the comment and the pass statement. Then add the highlighted code below:

63  def link_register_click(self, **event_args):
64    anvil.users.signup_with_form(allow_cancel=True)

Code explaination

  • line 64:

    • signup_with_form → use the built-in registration form

    • anvil.users. → from the Anvil user managment tool

    • allow_cancel=True → add a cancel button to the registration form

Launch your website and try to register an account.

register

Stop your website and check that your account has been created in the users table.

check user

Logout

Since we are remembering the user, we now need to be able to logout.

Go to the link_logout_click handler. Delete the comment and the pass statement. Then add the highlighted code below:

70  def link_logout_click(self, **event_args):
71    anvil.users.logout()

Code explaination

  • line 70:

    • logout() → use the built-in logout feature

    • anvil.users. → from the Anvil user management tool

Login

Finally we need to allow users already registered to login

Go to the link_login_click handler. Delete the comment and the pass statement. Then add the highlighted code below:

66  def link_login_click(self, **event_args):
67    anvil.users.login_with_form(allow_cancel=True)

Code explaination

  • line 67:

    • login_with_form → use the built-in login form

    • anvil.users. → from the Anvil user managment tool

    • allow_cancel=True → add a cancel button to the registration form

Test

Run the test

It is really hard to test if our user features work, as we have no indication if the user is logged in or not. To solve this we will add some testing code to the handlers.

63  def link_register_click(self, **event_args):
64    anvil.users.signup_with_form(allow_cancel=True)
65    print("Register", anvil.users.get_user())
66
67  def link_login_click(self, **event_args):
68    anvil.users.login_with_form(allow_cancel=True)
69    print("Login", anvil.users.get_user())
70
71  def link_logout_click(self, **event_args):
72    anvil.users.logout()
73    print("Logout", anvil.users.get_user())

Code explaination

All three are estentially the same, so I’ll just explain the first.

  • line 65:

    • print → send to the terminal

    • "Register" → the name of the link just clicked

    • anvil.users.get_user() → what the current user is

Launch your website, then:

  • Register another user

  • Logout

  • Login using your new user

Notice your terminal has three new messages:

terminal

Click on the Terminal tab and you should have the three messages below.

messages

Tidy up

If you received the same messages, your test passed.

Go back into the MainForm code and remove the three print statements.

Final code state

By the end of this tutorial your code should be the same as below:

Final MainForm

 1from ._anvil_designer import MainFormTemplate
 2from anvil import *
 3import anvil.tables as tables
 4import anvil.tables.query as q
 5from anvil.tables import app_tables
 6import anvil.users
 7from ..HomeComponent import HomeComponent
 8from ..CalendarComponent import CalendarComponent
 9from ..AddComponent import AddComponent
10from ..AccountComponent import AccountComponent
11
12
13class MainForm(MainFormTemplate):
14  def __init__(self, **properties):
15    # Set Form properties and Data Bindings.
16    self.init_components(**properties)
17    self.breadcrumb_stem = self.label_title.text
18
19    # Any code you write here will run before the form opens.
20    self.content_panel.add_component(HomeComponent())
21    self.set_active_link("home")
22
23  def set_active_link(self, state):
24    if state == "home":
25      self.link_home.role = "selected"
26    else:
27      self.link_home.role = None
28    if state == "add":
29      self.link_add.role = "selected"
30    else:
31      self.link_add.role = None
32    if state == "calendar":
33      self.link_calendar.role = "selected"
34    else:
35      self.link_calendar.role = None
36  
37  # --- link handlers
38  def link_home_click(self, **event_args):
39    self.content_panel.clear()
40    self.content_panel.add_component(HomeComponent())
41    self.label_title.text = self.breadcrumb_stem
42    self.set_active_link("home")
43
44  def link_calendar_click(self, **event_args):
45    self.content_panel.clear()
46    self.content_panel.add_component(CalendarComponent())
47    self.label_title.text = self.breadcrumb_stem + " - Calendar"
48    self.set_active_link("calendar")
49
50  def link_add_click(self, **event_args):
51    self.content_panel.clear()
52    self.content_panel.add_component(AddComponent())
53    self.label_title.text = self.breadcrumb_stem + " - Add"
54    self.set_active_link("add")
55
56  def link_account_click(self, **event_args):
57    """This method is called when the link is clicked"""
58    self.content_panel.clear()
59    self.content_panel.add_component(AccountComponent())
60    self.label_title.text = self.breadcrumb_stem + " - Account"
61    self.set_active_link(("account"))
62
63  def link_register_click(self, **event_args):
64    anvil.users.signup_with_form(allow_cancel=True)
65
66  def link_login_click(self, **event_args):
67    anvil.users.login_with_form(allow_cancel=True)
68
69  def link_logout_click(self, **event_args):
70    anvil.users.logout()